Home  >  Article  >  Backend Development  >  How to write queries in PHP 5.5.0

How to write queries in PHP 5.5.0

PHPz
PHPzOriginal
2023-04-19 11:36:21452browse

PHP is a popular programming language through which databases can be operated. Querying is a common task used to retrieve and obtain information stored in a database. In PHP 5.5.0, executing queries can be completed in the following three ways:

  1. Using mysql functions
    PHP 5.5.0 supports mysql functions by default, and you can use them to directly connect to the MySQL database and execute Query operations. The following is a basic example:
<?php
//建立连接
$conn = mysql_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;password&#39;);
//选择数据库
mysql_select_db(&#39;mydb&#39;);
//执行查询
$result = mysql_query(&#39;SELECT * FROM mytable&#39;);
//输出结果
while($row = mysql_fetch_assoc($result)) {
    echo $row[&#39;name&#39;] . &#39;, &#39; . $row[&#39;age&#39;] . "<br>";
}
//关闭连接
mysql_close($conn);
?>

However, it should be noted that using mysql functions requires enabling extensions. These functions have been deprecated in PHP7.0. It is recommended to use mysqli or PDO.

  1. Using the mysqli function
    Mysqli is a new extension for PHP 5.5.0 and above, which provides a more powerful and flexible query function compared to the mysql function. The following is a sample code that uses the mysqli function to execute a query:
<?php
//建立连接
$conn = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;password&#39;, &#39;mydb&#39;);
//执行查询
$result = mysqli_query($conn, &#39;SELECT * FROM mytable&#39;);
//输出结果
while($row = mysqli_fetch_assoc($result)) {
    echo $row[&#39;name&#39;] . &#39;, &#39; . $row[&#39;age&#39;] . "<br>";
}
//关闭连接
mysqli_close($conn);
?>

It should be noted that the parameters that need to be passed in the mysqli function represent the database address, user name, password, database name, and mysql in order. Functions are different. In addition, the mysqli function provides a safer query method, such as using prepared statement to execute queries, avoiding security issues such as SQL injection.

  1. Using PDO
    PDO is a new PDO extension for PHP 5.5.0 and above. It provides a query method that is independent of MySQL, PostgreSQL, Oracle and other database platforms, and is more convenient. Used across databases. The following is a sample code for using PDO to execute a query:
<?php
//建立连接
$dsn = "mysql:host=localhost;dbname=mydb";
$user = "root";
$password = "password";
$pdo = new PDO($dsn, $user, $password);
//执行查询
$stmt = $pdo->query('SELECT * FROM mytable');
//输出结果
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo $row['name'] . ', ' . $row['age'] . "<br>";
}
//关闭连接
$pdo = null;
?>

It should be noted that when using PDO, you need to first connect to the database through DSN (Data Source Name, data source name). PDO also provides convenient and safe query methods such as automatic escaping and precompiled statements. In addition, it should be noted that each row of data returned by PDO is an array, and the data can be obtained by specifying fetch_style and modifying it to an object or column name only.

The above are sample codes for three different query methods in PHP 5.5.0. It is very important to choose different extensions and query methods for different needs and database platforms. It should be noted that security issues such as SQL injection need to be prevented when querying. This is a necessary means to protect the security of database data.

The above is the detailed content of How to write queries in PHP 5.5.0. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn