Home > Article > Backend Development > How to set the time for PHP to connect to the database
PHP is a widely used server-side scripting language that can interact and communicate with various databases. In actual development, we often encounter situations where we need to connect to the database. There are many factors involved when PHP connects to the database, one of the important factors is the setting of the connection time. This article will briefly introduce how to set the time for PHP to connect to the database.
1. PHP database connection time setting
The time to connect to the database in PHP is generally controlled by two variables: connection timeout and query timeout. The connection timeout refers to the maximum waiting time to connect to the database. If the database cannot be connected within this time, the connection request will fail; the query timeout refers to the maximum waiting time for query execution. If within this time, the connection request will fail. If the query cannot be completed, execution will be abandoned and an error message will be returned.
After PHP 5.3, you can set the connection timeout and query timeout by adding the timeout keyword in the parameters. An example is as follows:
$conn = mysqli_connect($host, $username, $password, $dbname, $port, null, array('connect_timeout' => $connect_timeout, 'query_timeout' => $query_timeout));
Among them, $connect_timeout and $query_timeout represent the connection timeout and query timeout respectively, in seconds.
In addition, in PDO, you can also control the connection time by setting attributes. The example is as follows:
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::ATTR_TIMEOUT => $timeout));
Among them, $timeout represents the timeout time in seconds.
2. How to set the connection timeout and query timeout
In PHP, the connection timeout can be set in the parameter in settings. The sample code is as follows:
$conn = mysqli_connect($host, $username, $password, $dbname, $port, null, array('connect_timeout' => $connect_timeout, 'query_timeout' => $query_timeout));
Among them, $connect_timeout represents the connection timeout, in seconds; $query_timeout represents the query timeout, in seconds. We can set the values of these two parameters as needed.
The query timeout setting in PHP generally involves two parameters of MySQL: wait_timeout and interactive_timeout. wait_timeout refers to the time the client waits for the server to close the connection in the inactive state; interactive_timeout refers to the time the client waits for the server to close the connection in the inactive state. It will work if and only if the client uses the "INTERACTIVE" option. . By default, the value of these two parameters is 28800 seconds, which is 8 hours.
In PHP, we can control the execution time of the query by specifying the timeout in the SQL statement. The sample code is as follows:
$stmt = $conn->prepare("SELECT * FROM users WHERE id=?"); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->store_result(); $stmt->sqlstate(); $stmt->get_warnings(); $stmt->errno(); $stmt->error(); $stmt->fetch();
When executing the query statement, we can add the following code:
$stmt->query("SET SESSION wait_timeout=120");
In this way, the timeout can be set here, here we set it to 120 seconds. Since wait_timeout is a server-side parameter, the setting of this timeout is rough and not detailed enough.
3. Summary
When PHP connects to the database, you need to pay attention to the setting of the connection time, including the connection timeout and query timeout. We can achieve this by specifying the timeout in the parameters; in addition, we can also specify the timeout in the SQL statement to limit the execution time of the query. For more detailed timeout control, a deeper understanding of the MySQL server-side parameters is required. In actual development, we need to set the timeout reasonably according to the specific situation to ensure the stability and reliability of the system.
The above is the detailed content of How to set the time for PHP to connect to the database. For more information, please follow other related articles on the PHP Chinese website!