A variety of web applications can be developed using the popular open source programming language PHP. This article explains how to use PHP to query MySQL database tables. Many web applications use MySQL, a popular relational database management system, to store and manage data. By using PHP and MySQL, you can easily store data in a database and access it easily through web applications. In order to start using PHP to query a MySQL data table, you need to first create a PHP script that connects to the MySQL database. Here is a simple PHP script that can be used to connect to a MySQL database: ``` //Set database connection information $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database"; //Create database connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check if the connection is successful if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connection successful"; ``` Replace the `$servername`, `$username`, `$password` and `$dbname` variables in the above code with your MySQL database connection information. Running this script will output "Connection successful" if the connection is successful, otherwise it will output an error message. connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>In this example, we use the mysqli class to connect with the MySQL database. Please note that the die() function in the code will stop the execution of the PHP script if the connection is unsuccessful. Once you have established a connection, you can start querying the data in the MySQL database. In PHP, you can use the mysqli_query() function to perform queries. The following code shows how to query all data in the MySQL data table users. connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; // 查询数据表中的所有数据 $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); // 输出每一行数据 while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - 姓名: " . $row["name"]. " - 邮箱: " . $row["email"]. ""; } ?>In the above code, we use the mysqli_query() function to execute the query, and then use the mysqli_fetch_assoc() function to iterate through the query results. Note that we used * in the SQL query to indicate selecting all columns. You can use other query statements that select specific columns. For example, the following code selects the name and email columns in the data table: $sql = "SELECT name,email FROM users";In addition to selecting against specific columns, you can also use the complete WHERE sub Sentence to filter specific rows. For example, the following code will select all records named "John": $sql = "SELECT * FROM users WHERE name='John'";In this example, the WHERE clause specifies the record named "John". You can further filter the data using other operators such as greater than, less than, equal to, etc. Here are some examples: // 选择所有年龄大于 18 岁的记录 $sql = "SELECT * FROM users WHERE age > 18"; // 选择所有邮箱以 @gmail.com 结尾的记录 $sql = "SELECT * FROM users WHERE email LIKE '%@gmail.com'"; // 选择前五条数据 $sql = "SELECT * FROM users LIMIT 5"; In this example we are using operators like > and LIKE, and LIMIT clause to limit the amount of data. You can use other similar operators and clauses to perform more complex queries.