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

Source: Internet
Author: User

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

Recently, due to project requirements
You need to use the sleep function to regularly fetch a pile of data from the database to perform some operations.
Sleep waits for at least one hour.
Previously tested
It is feasible to use the sleep function to perform operations after several hours.
 
The problem is wrong.
When the program uses sleep, it finds that the corresponding information cannot be obtained from the database.
Remove sleep
The result is normal.
 
Depressed...
Does sleep affect database reading !!!
So in order to facilitate the test
Run the sleep command 10 seconds later.
The result can read information from the database.
 
But why can't I read the information one hour after sleep?
For testing convenience, I read the database once before the sleep statement, and then read the database again after the sleep statement.
For example:
Copy codeThe Code is as follows:
<? Php
Require_once ('include. php ');
// Read database information
$ Data = $ db-> getList ();
Print_r ($ data );
 
// One hour later
Sleep (3600 );
 
// Read the information again
$ Data = $ db-> getList ();
Print_r ($ data );
 
?>
Result discovery
The first database read is successful.
The second read database is empty.
 
So I changed sleep to 10 seconds and then tested it again.
Copy codeThe Code is as follows:
<? Php
Require_once ('include. php ');
// Read database information
$ Data = $ db-> getList ();
Print_r ($ data );
 
// After 10 seconds
Sleep (10 );
 
// Read the information again
$ Data = $ db-> getList ();
Print_r ($ data );
 
?>

The above results
The database is successfully read twice.
 
Why did an hourly database fail to be read, but the database was successfully read in 10 seconds ??
I am using a single instance database operation class.
Think of a problem
Will reading fail due to database connection timeout?
So I need to change the database reading operation to the current connection.
Copy codeThe Code is as follows:
<? Php
Require_once ('include. php ');
// Read database information
$ Data = getList ();
Print_r ($ data );
 
// 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 !!
The original sleep may cause a single-instance timeout problem. If the execution time is too long, the database connection may be disconnected, and the database information cannot be read!


Why does php fail to save the source code to MYSQL after reading the webpage?

Some special characters are not processed. You can use addslashes () to convert them.
If it doesn't work, use base64_encode () to encrypt it and save it again.
Use base64_decode () for decryption.

How to read mysql from php

The SQL statement writes the select name for table name where...
Then read 0th

Or you can use the Function
Mysql_fetch_array

Example 2. mysql_fetch_array use MYSQL_NUM

<? Php
Mysql_connect ("localhost", "mysql_user", "mysql_password") or
Die ("cocould 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 );
?>

Example 3. mysql_fetch_array use MYSQL_ASSOC

<? Php
Mysql_connect ("localhost", "mysql_user", "mysql_password") or
Die ("cocould 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 );
?>

Example 4. mysql_fetch_array use MYSQL_BOTH

<? Php
Mysql_connect ("localhost", "mysql_user", "mysql_password") or
Die ("cocould 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",... full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.