Home >Backend Development >PHP Tutorial >Use the PHP function 'mysqli_connect' to establish a connection to the MySQL database
Use the PHP function "mysqli_connect" to establish a connection with the MySQL database
PHP is a scripting language widely used in web development that can interact with the database. MySQL is currently the most popular open source relational database management system in the world. In PHP, we can use the "mysqli_connect" function to establish a connection to the MySQL database.
First, we need to ensure that the MySQL server is installed and running. We can then follow the steps below to establish a connection to the MySQL database in PHP.
Step 1: Create database connection parameters
First, we need to define some necessary variables, including the host name, user name, password and database name of the MySQL server. These parameters will be used to establish a connection to the database.
$host = 'localhost'; // MySQL服务器的主机名 $username = 'root'; // 用户名 $password = '123456'; // 密码 $database = 'mydatabase'; // 数据库名称
Please modify the values of these parameters according to the actual situation.
Step 2: Establish a database connection
Next, we can use the "mysqli_connect" function to establish a connection to the MySQL database.
$connection = mysqli_connect($host, $username, $password, $database); // 检查连接是否成功 if (!$connection) { die("连接失败: " . mysqli_connect_error()); }
In this example, we will create a variable named $connection to save the database connection. First, we pass the previously defined parameters (hostname, username, password, and database name) to the "mysqli_connect" function. If the connection is successful, a connection object will be returned; if the connection fails, an error message will be returned.
We can use the "mysqli_connect_error" function to get the error message when the connection fails, and use the "die" function to terminate the execution of the script.
Step 3: Use database connection to operate
Now, we have successfully established a connection with the MySQL database and can perform various database operations, such as querying data, inserting data, updating data, etc. wait.
The following is a simple example showing how to query data in the database:
$query = "SELECT * FROM users"; $result = mysqli_query($connection, $query); // 检查查询是否成功 if (!$result) { die("查询失败: " . mysqli_error($connection)); } // 输出查询结果 while ($row = mysqli_fetch_assoc($result)) { echo "ID: " . $row['id'] . ", 姓名: " . $row['name'] . ", 年龄: " . $row['age'] . "<br>"; } // 释放结果集资源 mysqli_free_result($result);
In this example, we first define a string variable $query containing an SQL statement for querying User information in the database. We then execute this query using the "mysqli_query" function and save the result in a variable $result.
Next, we use a loop to traverse the result set and use the "mysqli_fetch_assoc" function to obtain the data for each row. Finally, we output the information for each row and release the result set resources using the "mysqli_free_result" function.
Step 4: Close the database connection
When we have completed the interaction with the MySQL database, we should close the database connection and release resources.
mysqli_close($connection);
In this example, we use the "mysqli_close" function to close the database connection saved in the $connection variable.
Summary:
By using the PHP function "mysqli_connect" you can easily establish a connection with the MySQL database and implement various operations on the database. During the connection process we need to provide the appropriate hostname, username, password and database name. After the connection is successful, we can use the "mysqli_query" function to execute SQL queries and use the various "mysqli_fetch_" functions to obtain the query results. Finally, we should use the "mysqli_close" function to close the database connection and release resources.
What is provided here is a basic connection and query example, which can be modified and expanded according to actual needs. I hope this article helps you understand how to use the PHP function "mysqli_connect" to establish a connection to a MySQL database.
The above is the detailed content of Use the PHP function 'mysqli_connect' to establish a connection to the MySQL database. For more information, please follow other related articles on the PHP Chinese website!