Home  >  Article  >  Backend Development  >  PHP adds query database statement

PHP adds query database statement

王林
王林Original
2023-05-07 09:16:06386browse

PHP is a popular server-side programming language that is widely used in web development. In web development, it is often necessary to interact with the database, and the most common operation is to query the database. This article will introduce how to use PHP to add query database statements.

  1. Connecting to the database

In PHP, to use a database, you first need to establish a connection with the database. To connect to the database, you need to use the mysqli_connect function. The usage of this function is as follows:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

//创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);

//检查连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

In the above code, $servername, $username, $password and $dbname are the parameters needed to connect to the database. $servername refers to the server name of the database, $username refers to the user name of the database, $password refers to the user password of the database, and $dbname refers to the name of the database to be connected. If the connection fails, the mysqli_connect_error function will return a description of the error.

  1. Query database

To query the database, you need to use the SELECT statement. The SELECT statement retrieves data from the database and returns it to PHP. The following is the basic syntax of the SELECT statement:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

In the above code, column1, column2 represent the column names of the database table to be retrieved, table_name represents the name of the database table to be retrieved, condition is optional, use Used to specify what data to return.

The following is a sample SELECT statement:

SELECT id, firstname, lastname
FROM employees
WHERE lastname='Smith';

In the above code, the id, firstname and lastname columns in the employees table are returned, where lastname is equal to "Smith".

  1. Execute query statements in PHP

To execute query statements in PHP, you need to use the mysqli_query function. This function accepts two parameters: the $conn parameter to connect to the database and the $sql parameter containing the query statement to be executed. The following is how to use the mysqli_query function:

$sql = "SELECT id, firstname, lastname FROM employees WHERE lastname='Smith'";
$result = mysqli_query($conn, $sql);

In the above code, the $sql variable contains the query statement, and the $result variable holds the results of the query.

  1. Processing query results

To process the query results, you need to use the mysqli_fetch_assoc function to return the results as an associative array. The following is an example of using the mysqli_fetch_assoc function:

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

In the above code, use the mysqli_num_rows function to check whether the query result is empty. If the result is not empty, use the mysqli_fetch_assoc function to fetch the rows of the result and return them as an associative array.

  1. Close the connection

After completing the query, you need to use the mysqli_close function to close the connection with the database to release resources. The following is an example of using the mysqli_close function:

//关闭连接
mysqli_close($conn);

In the above code, the $conn parameter is the variable name connected to the database, and the mysqli_close function closes the connection and releases resources.

Summary

In PHP, to query the database, you need to connect to the database first, and then use the SELECT statement to execute the query. To execute query statements in PHP, you can use the mysqli_query function. To process query results, you can use the mysqli_fetch_assoc function. After completing the query, you need to use the mysqli_close function to close the connection and release resources. With the methods described in this article, you can easily query your database using PHP.

The above is the detailed content of PHP adds query database statement. 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