Home > Backend Development > PHP Problem > How to set the time for PHP to connect to the database

How to set the time for PHP to connect to the database

PHPz
Release: 2023-04-06 13:16:01
Original
1397 people have browsed it

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));
Copy after login
Copy after login

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));
Copy after login

Among them, $timeout represents the timeout time in seconds.

2. How to set the connection timeout and query timeout

  1. Setting the connection 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));
Copy after login
Copy after login

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.

  1. Query timeout setting

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();
Copy after login

When executing the query statement, we can add the following code:

$stmt->query("SET SESSION wait_timeout=120");
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template