PHP database query tips: How to use the mysqli_query function to perform SQL queries
When developing PHP applications, interaction with the database is a very important part. For query operations, PHP provides some built-in functions to execute SQL statements. This article will focus on how to use the mysqli_query function to help developers better perform database query operations.
1. Introduction to mysqli_query function
The mysqli_query function is a built-in function of PHP, used to execute SQL query statements. Its basic syntax is as follows:
mysqli_query($连接对象, $SQL语句);
Among them, the $connection object is the database connection object created through the mysqli_connect function, and the $SQL statement is the query operation statement to be executed.
2. Use the mysqli_query function to perform simple queries
The following is a simple example that demonstrates how to use the mysqli_query function to perform query operations:
<?php // 创建数据库连接 $connection = mysqli_connect("localhost", "用户名", "密码", "数据库名"); // 检查连接是否成功 if (!$connection) { die("数据库连接失败:" . mysqli_connect_error()); } // 执行查询语句 $result = mysqli_query($connection, "SELECT * FROM users"); // 遍历查询结果 while ($row = mysqli_fetch_assoc($result)) { echo "ID:" . $row["id"] . ",姓名:" . $row["name"] . ",年龄:" . $row["age"] . "<br>"; } // 关闭数据库连接 mysqli_close($connection); ?>
In the above code, first use the mysqli_connect function Created a database connection object $connection. Then, a simple query statement "SELECT * FROM users" is executed using the mysqli_query function, and the query results are saved in the variable $result. Finally, use the mysqli_fetch_assoc function to fetch a row of records from $result and operate on it.
3. Processing query results
After the mysqli_query function executes the query statement, the returned results can be processed in a variety of ways. Commonly used methods include the mysqli_fetch_assoc function, mysqli_fetch_array function and mysqli_fetch_row function.
When using these functions, you need to use loop statements to process the query results row by row to retrieve all records.
4. Exception handling of query results
When executing query operations, errors sometimes occur, such as syntax errors, database connection interruptions, etc. To avoid these errors causing the application to crash, we need to handle errors appropriately. The error information of the query operation can be obtained through the mysqli_error function, as shown below:
<?php $result = mysqli_query($connection, $sql); if (!$result) { die("查询失败:" . mysqli_error($connection)); } ?>
In the above example, if the query fails, we obtain the error information through the mysqli_error function and print it out using the die function. In this way, problems can be discovered and solved in time.
5. Security considerations
When performing database queries, we also need to consider security issues to prevent malicious injection attacks. In order to avoid this situation, you can use the mysqli_real_escape_string function to escape the data entered by the user, as shown below:
<?php $name = mysqli_real_escape_string($connection, $_POST["name"]); $sql = "SELECT * FROM users WHERE name='$name'"; $result = mysqli_query($connection, $sql); ?>
In the above example, we use the mysqli_real_escape_string function to escape the $name entered by the user. Escape and then use it as part of the query statement. This ensures that the data entered by the user does not destroy the structure of the query statement, thereby avoiding SQL injection attacks.
Summary:
This article mainly introduces how to use the mysqli_query function to perform SQL query operations. By mastering the basic usage of the mysqli_query function, developers can operate the database more conveniently and improve development efficiency. In addition, when performing database queries, attention must be paid to exception handling and security considerations to ensure the stability and security of the system. I hope this article will be helpful to your database queries in PHP development.
The above is the detailed content of PHP database query tips: How to use the mysqli_query function to perform SQL queries. For more information, please follow other related articles on the PHP Chinese website!