Home >Backend Development >PHP Problem >Let's talk about how MySQL+PHP executes query command line

Let's talk about how MySQL+PHP executes query command line

PHPz
PHPzOriginal
2023-04-06 09:14:46732browse

MySQL and PHP are two very common and widely used open source software. MySQL is a relational database management system, while PHP is a scripting language used to create web applications. When you need to extract information from a MySQL database, you can use PHP scripts to write query statements. In this article, we will take a deep dive into how to execute command line queries in PHP using MySQL.

Introduction to MySQL and PHP

First, let’s give a brief introduction to MySQL and PHP. MySQL is an open source relational database management system and one of the most popular open source relational databases. It can be applied on various types of applications such as web applications, enterprise applications, data warehouses, etc. At the same time, it also supports transaction processing and multi-user concurrent processing.

PHP is a scripting language used to create web applications. It is often used in server-side web programming and can be embedded in HTML and directly interpreted and executed by the web server. PHP is increasingly used in conjunction with MySQL because MySQL provides an effective, scalable, and efficient way to store and access data, and PHP provides a flexible, easy-to-use method for sending data to MySQL. Query and process results.

In the following content, we will focus on how to use MySQL queries in the command line of PHP.

Using the PHP command line for MySQL queries

There are many ways to connect to MySQL and execute queries in PHP, one of which is to use the command line interface. The PHP command line interface provides a simple and efficient way to interact with the MySQL database. Here are the steps on how to make a MySQL query in PHP command line:

  1. Open PHP command line or terminal window: Open a command line or terminal window as administrator.
  2. Connect to MySQL database: Enter the following command to connect to MySQL database:
mysql -u <username> -p

Where, <username> is the MySQL database user name. After entering this command, you will be prompted for a password. After entering the correct password, you will enter the MySQL command line window.

  1. Select the database to be queried: Enter the following command to select the database to be queried:
use <database_name>

Among them, <database_name> is the one to be queried Name database. Note that before this, the database to be queried must be created.

  1. Execute MySQL query: Enter the following command to execute MySQL query:
SELECT * FROM <table_name>;

Where, <table_name> is the name of the data table to be queried. This will query and return all records in the table. If you need to query records under specified conditions, you can add the WHERE statement to the query statement to specify the query conditions.

  1. Exit the MySQL command line: After executing the query, enter the following command to exit the MySQL command line:
exit;

Through the above steps, we can execute MySQL in the PHP command line Inquire. However, this method is cumbersome and requires manually entering each command. Fortunately, we can automate these commands and execute queries more easily by using PHP scripts.

Using PHP scripts to perform MySQL queries

Using PHP scripts to perform MySQL queries requires connecting to the MySQL database, sending the query, and processing the results. The following are the steps on how to use a PHP script to execute a MySQL query:

  1. Connect to the MySQL database: Enter the following PHP code to connect to the MySQL database:
<?php
$dbhost = &#39;localhost:3306&#39;;  // MySQL 数据库主机
$dbuser = &#39;root&#39;;  // MySQL 数据库用户名
$dbpass = &#39;password&#39;;  // MySQL 数据库密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
   die(&#39;Could not connect: &#39; . mysqli_error());
}
echo &#39;Connected successfully&#39;;
?>

Where, $dbhost is the MySQL database host name and port number, $dbuser is the MySQL database user name, $dbpass is the MySQL database password. If the connection fails, the "Could not connect" error message will be output; if the connection is successful, the "Connected successfully" message will be output. At this point, you are connected to the MySQL database and can send queries.

  1. Send MySQL query: Enter the following PHP code to send MySQL query:
<?php
$sql = 'SELECT * FROM <table_name>';
mysqli_select_db($conn, '<database_name>');  // 选择要查询的数据库
$retval = mysqli_query($conn, $sql);
if(! $retval ) {
   die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
   echo "EMP ID :{$row['emp_id']}  <br> ".
      "EMP NAME : {$row['emp_name']} <br> ".
      "EMP SALARY : {$row['emp_salary']} <br> ".
      "--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysqli_close($conn);
?>

Among them, $sql is the query statement to be sent, <table_name> is the name of the data table to be queried, <database_name> is the name of the database to be queried. This will select the database to query and execute the query statement, returning all records that meet the criteria. If the query fails, the "Could not get data" error message will be output; if the query is successful, all records that meet the conditions will be output. At this point, the query has been completed and the results have been processed, and the connection can be closed.

  1. Close the MySQL connection: Enter the following PHP code to close the MySQL connection:
<?php
mysqli_close($conn);
?>

Through the above steps, we can use PHP scripts to execute MySQL queries. This method is more convenient than manually entering the command line, and can automate the query process and process the query results.

Conclusion

MySQL and PHP are two very powerful and widely used open source software. When you need to extract information from a MySQL database, you can use PHP scripts to write query statements. This article hopes to help readers by introducing the basic knowledge of MySQL and PHP, as well as how to execute MySQL queries in PHP's command line and scripts.

The above is the detailed content of Let's talk about how MySQL+PHP executes query command line. 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