Cases and solutions for mysql reading failure caused by using sleep in PHP, sleepmysql_PHP tutorial

WBOY
Release: 2016-07-13 10:20:18
Original
895 people have browsed it

Case and solution for mysql reading failure caused by using sleep in PHP, sleepmysql

Recently, due to project needs
You need to use the sleep function to regularly fetch a bunch of data from the database to perform certain operations.
The waiting time for sleep is at least an hour
Tested before
It is possible to use the sleep function to complete operations performed several hours later

But here comes the evil problem
After using sleep, the program found that it could not get the corresponding information from the database
Remove sleep
The result is normal

Depressed. . .
Does sleep affect the library reading operation? ! !
So for the convenience of testing
Just do sleep(10) and execute it in ten seconds
The result can read information from the database

But why can't sleep() read information after an hour?
For the convenience of testing, I read the library directly before the sleep statement, and read the library again after the sleep statement
Such as:

Copy code The code is as follows:

require_once('include.php');
//Read database information
$data = $db->getList();
print_r($data);

// Schedule one hour later
sleep(3600);

// Read the information again
$data = $db->getList();
print_r($data);

?>

It turns out
Successfully read the database for the first time
The second read library is empty

So I changed sleep to ten seconds and then tested again
Copy code The code is as follows:

require_once('include.php');
//Read database information
$data = $db->getList();
print_r($data);

// Timer after ten seconds
sleep(10);

// Read the information again
$data = $db->getList();
print_r($data);

?>

The above results
Successfully read the database twice

Why does it fail to read the database after one hour, but successfully after ten seconds? ?
I use the singleton database operation class
A question comes to mind
Could it be that the database connection timed out and caused the database reading to fail?
So I quickly changed the database reading operation here to a real-time connection
Copy code The code is as follows:

require_once('include.php');
//Read database information
$data = getList();
print_r($data);

// Schedule one hour later
sleep(3600);

// Read the information again
$data = getList();
print_r($data);

//Read database information
function getList(){
           $pdo = new PDO('mysql:host=localhost;dbname=test','root','root');
          $result = $pdo->query('select * from tables');
          return $result->fetchAll(PDO::FETCH_ASSOC);
}
?>

Test successful! !
It turns out that sleep will cause the singleton class to time out, and the database connection may be disconnected after the execution time is too long, and the database information cannot be read!

Failed to save the source code into MYSQL after php reads the web page?

There should be some special characters that are not processed. You can use addslashes() to convert them.
If that doesn’t work, use base64_encode() to encrypt it and save it again.
When used, just take it out and use base64_decode() to decrypt it.

For the problem of php reading mysql

sql语句这样写select name for 表名 where...
然后读取第0个

或者你可以用函数
mysql_fetch_array

例子 2. mysql_fetch_array 使用 MYSQL_NUM

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>

例子 3. mysql_fetch_array 使用 MYSQL_ASSOC

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?>

例子 4. mysql_fetch_array 使用 MYSQL_BOTH

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s Name: %s",......余下全文>>
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/867253.htmlTechArticlePHP中使用sleep造成mysql读取失败的案例和解决方法,sleepmysql 近日,由于项目需求 需要用到sleep函数定时从数据库取一堆数据出来去执行某些...
Related labels:
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