PHP is a commonly used server-side programming language for developing web applications. During the development process, we often need to store data in a database. MySQL is one of the popular MySQL relational database management systems. Therefore, it is very important to learn how to connect PHP to a MySQL database.
In this article, we will focus on how to connect PHP to the MySQL database, and introduce some basic steps and techniques to allow you to connect to the database easily and happily.
Before connecting to the database
Before connecting to the database, we need to define some constants and variables about the database, for example:
$hostname = "localhost"; $username = "root"; $password = ""; $database = "demo";
$hostname
: Indicates the server address where the database is located$username
: Indicates the username to connect to the database$password
: Indicates the password to connect to the database$database
: Indicates the name of the database we want to connect toYou can put these constants and variables into a separate PHP file, such as config.php file and then include it in other PHP files for easy reference and maintenance.
Steps to connect to the database
The steps to connect to the MySQL database are usually divided into the following three steps:
To connect with To establish a connection to the MySQL database, we can use PHP's built-inmysqli_connect()
function. This function needs to pass 4 parameters, namely the server address where the database is located, user name, password and database name.
For example, we can use the following code to connect to the database:
$conn = mysqli_connect($hostname, $username, $password, $database);
If the connection is successful, the function will return a connection ID, otherwise it will returnfalse
.
We can use themysqli_query()
function to query data. This function needs to pass two parameters, the first parameter is the connection ID, and the second parameter is the SQL command to be queried.
For example, we can use the following code to query all data in the user table:
$sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql);
This function returns the query results to the$result
variable.
When we complete the operation on the database, we should close the connection to release resources. We can use themysqli_close()
function to close the connection.
For example, we can use the following code to close the connection:
mysqli_close($conn);
Complete example
To help you better understand how PHP connects to the MySQL database, we provide a Complete example.
For example, suppose we create a table namedusers
in the demo database, which contains two fieldsid
andname
, we can use the following code to query all the data in this table:
0) { while ($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"] . " - 姓名: " . $row["name"] . "
"; } } else { echo "0 结果"; } // 关闭连接 mysqli_close($conn); ?>
Note that the above example is for demonstration purposes only, and should be more rigorous in actual use. For example, user input should be filtered and the query statement should be checked whether it is executed. Success etc.
Conclusion
This article briefly introduces the basic steps for PHP to connect to the MySQL database, including establishing a connection, querying data, and closing the connection. I hope this article can help you better understand and master how to use PHP to connect to a MySQL database. In actual development, there are more details and techniques that need to be paid attention to, and we look forward to your exploration and discovery.
The above is the detailed content of How to connect php to MySQL database?. For more information, please follow other related articles on the PHP Chinese website!