Home > Article > Backend Development > PHP database query source code
PHP is a widely used open source scripting language, which is commonly used to develop web applications. Database querying is an important task in web development because most applications require retrieving information from a database and then rendering it as content on a web page. In PHP, using database queries requires connecting to the database and executing a SQL query. Here are some common PHP database query source codes.
Before using PHP to query the database, you need to connect to the database first. The following is the PHP code to connect to the MySQL database:
$host = 'localhost'; // 数据库主机名 $user = 'root'; // 数据库用户名 $pass = ''; // 数据库密码 $dbname = 'mydatabase'; // 数据库名称 // 创建连接 $conn = mysqli_connect($host, $user, $pass, $dbname); // 检查连接 if (!$conn) { die('连接数据库失败:' . mysqli_connect_error()); }
In this example, we use the mysqli_connect() function to create a connection to the MySQL database. If the connection fails, we use the mysqli_connect_error() function to output an error message and stop script execution.
After connecting to the database, you can execute SQL queries. The following is a simple query example:
$sql = 'SELECT * FROM users'; $result = mysqli_query($conn, $sql); if (!$result) { die('查询失败:' . mysqli_error($conn)); }
In this example, we define a SQL statement to query all columns in the table named "users". We then execute this query using the mysqli_query() function and save the result in the $result variable. Finally, if the query fails, we use the mysqli_error() function to output an error message and stop script execution.
Once the query has been executed, you can start processing the results. The following is an example of processing query results:
while ($row = mysqli_fetch_assoc($result)) { echo 'ID: ' . $row['id'] . '<br>'; echo 'Name: ' . $row['name'] . '<br>'; echo 'Email: ' . $row['email'] . '<br>'; echo '<hr>'; }
In this example, we use the mysqli_fetch_assoc() function to retrieve the query results row by row. Each query result row is stored in the $row variable, which is an associative array where the key is the column name and the value is the value of that column in that row. Then, we use the echo statement to output the results to the web page.
Finally, it is recommended to close the database connection immediately after completing the query to ensure security and performance. The following is an example of closing a connection:
mysqli_close($conn);
In this example, we use the mysqli_close() function to close the connection.
To sum up, this is a simple example of PHP querying MySQL database. It covers the basic steps of connecting to a database, executing queries, and processing query results. Of course, there are many complex queries and more advanced techniques that can be used for database queries in PHP development, but this example provides a starting point and will hopefully help you get started querying your database.
The above is the detailed content of PHP database query source code. For more information, please follow other related articles on the PHP Chinese website!