PHP中使用sleep造成mysql读取失败的案例和解决方法,sleepmysql
PHP中使用sleep造成mysql读取失败的案例和解决方法,sleepmysql
近日,由于项目需求
需要用到sleep函数定时从数据库取一堆数据出来去执行某些操作。
sleep等待的时间至少有一个小时以上
此前做过测试
用sleep函数去完成数小时后执行的操作是可行的
可邪门的问题出来了
程序用sleep后发现不能从数据库取到相应的信息
把sleep去掉
结果正常
郁闷中。。。
难道sleep影响读库操作!!!
于是为了方便测试
直接来个sleep(10) 十秒后执行
结果能从数据库读取信息
可为什么sleep()一个小时后不能读取信息呢?
为了测试方便我直接在sleep语句前读库一次,sleep后再读一次库
如:
复制代码 代码如下:
require_once('include.php');
// 读取数据库信息
$data = $db->getList();
print_r($data);
// 定时一个小时以后
sleep(3600);
// 再读取一次信息
$data = $db->getList();
print_r($data);
?>
结果发现
第一次读库成功
第二次读库为空
于是再把sleep改成十秒钟后再测试一次
复制代码 代码如下:
require_once('include.php');
// 读取数据库信息
$data = $db->getList();
print_r($data);
// 定时十秒以后
sleep(10);
// 再读取一次信息
$data = $db->getList();
print_r($data);
?>
以上结果
两次读库成功
为何一个小时读库失败,十秒钟却读库成功呢??
我用的是单例数据库操作类
想起一个问题
会不会是数据库连接超时导致读库失败呢?
于是赶紧把此处读库操作改成现连
复制代码 代码如下:
require_once('include.php');
// 读取数据库信息
$data = getList();
print_r($data);
// 定时一个小时以后
sleep(3600);
// 再读取一次信息
$data = getList();
print_r($data);
// 读取数据库信息
function getList(){
$pdo = new PDO('mysql:host=localhost;dbname=test','root','root');
$result = $pdo->query('select * from tables');
return $result->fetchAll(PDO::FETCH_ASSOC);
}
?>
测试成功!!
原来sleep会导致单例类超时问题从而出现执行时间过长以后数据库连接可能断开问题,也就不能读到数据库信息!
应该是有些特殊字符没处理,你用addslashes()转换一下。
如果实在不行,就用base64_encode()加密一下再存。
用的时候取出来用base64_decode()解密就行了。
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",......余下全文>>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example
