Examples and workarounds for MySQL read failure using sleep in PHP sleepmysql_php tutorial

Source: Internet
Author: User
Tags sleep function

Use sleep in PHP to cause MySQL read failure cases and workarounds, Sleepmysql


Recently, due to project demand
The sleep function is needed to periodically fetch a heap of data from the database to perform certain operations.
Sleep waits at least one hours
Previous tests have been made
It is possible to use the sleep function to complete the operation after a few hours

It's a scary question.
The program uses sleep to find that the corresponding information cannot be taken from the database
Remove the sleep.
Results are normal.

Depressed in ...
Does sleep affect read library operations!!!
So for the convenience of testing
Just a sleep. (10) 10 seconds after execution
Results can read information from the database

Why is sleep () Unable to read information after one hours?
For testing convenience I read the library directly before the sleep statement, and then read the library again after sleep
Such as:
Copy CodeThe code is as follows:
<?php
Require_once (' include.php ');
Read database Information
$data = $db->getlist ();
Print_r ($data);

One hours after the scheduled time
Sleep (3600);

Read the message again
$data = $db->getlist ();
Print_r ($data);

?>
Results found
First Read Library success
Second Read library is empty

And then you change the sleep to 10 seconds and then test again.
Copy CodeThe code is as follows:
<?php
Require_once (' include.php ');
Read database Information
$data = $db->getlist ();
Print_r ($data);

10 seconds after the timer.
Sleep (10);

Read the message again
$data = $db->getlist ();
Print_r ($data);

?>

Above results
Two Read library success

Why did the one-hour library fail, 10 seconds to read the library successfully??
I'm using a singleton database operation class
Think of a problem
Will the database connection timeout cause the library to fail?
So we quickly change the library operation here to the current connection
Copy CodeThe code is as follows:
<?php
Require_once (' include.php ');
Read database Information
$data = GetList ();
Print_r ($data);

One hours after the scheduled time
Sleep (3600);

Read the message 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 success!!
The original sleep will cause the Singleton class timeout problem so that the execution time is too long after the database connection may be disconnected problems, you can not read the database information!


PHP failed to save source code after reading Web page?

It should be some special characters not handled, you use Addslashes () conversion.
If not, use Base64_encode () to encrypt and then save.
Use the Base64_decode () to decrypt it.

For PHP to read MySQL problem

SQL statement so write select name for table name where ...
And then read a No. 0

Or you can use the function
Mysql_fetch_array

Example 2. Mysql_fetch_array using 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);
?>

Example 3. Mysql_fetch_array using 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);
?>

Example 4. Mysql_fetch_array using 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",...... Remaining full text >>

http://www.bkjia.com/PHPjc/867253.html www.bkjia.com true http://www.bkjia.com/PHPjc/867253.html techarticle PHP use sleep to cause MySQL read failure cases and solutions, sleepmysql recently, because the project needs to use the sleep function timed from the database to take a bunch of data out to perform some ...

  • 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.