PHP访问MySQL查询超时处理(转载)
时间:2011-05-30 来源:flyhacker
//自己定义读写超时常量
if (!defined('MYSQL_OPT_READ_TIMEOUT')) {
define('MYSQL_OPT_READ_TIMEOUT', 11);
}
if (!defined('MYSQL_OPT_WRITE_TIMEOUT')) {
define('MYSQL_OPT_WRITE_TIMEOUT', 12);
}
//设置超时
$mysqli = mysqli_init();
$mysqli->options(MYSQL_OPT_READ_TIMEOUT, 3);
$mysqli->options(MYSQL_OPT_WRITE_TIMEOUT, 1);
//连接数据库
$mysqli->real_connect("localhost", "root", "123456", "discuz");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//执行查询 sleep 1秒不超时
printf("Host information: %s\n", $mysqli->host_info);
if (!($res=$mysqli->query('select sleep(1)'))) {
echo "query1 error: ". $mysqli->error ."\n";
} else {
echo "Query1: query success\n";
}
//执行查询 sleep 9秒会超时
if (!($res=$mysqli->query('select sleep(9)'))) {
echo "query2 error: ". $mysqli->error ."\n";
} else {
echo "Query2: query success\n";
}
$mysqli->close();
echo "close mysql connection\n";
作者:http://blog.csdn.net/heiyeshuwu/archive/2010/09/08/5869813.aspx
if (!defined('MYSQL_OPT_READ_TIMEOUT')) {
define('MYSQL_OPT_READ_TIMEOUT', 11);
}
if (!defined('MYSQL_OPT_WRITE_TIMEOUT')) {
define('MYSQL_OPT_WRITE_TIMEOUT', 12);
}
//设置超时
$mysqli = mysqli_init();
$mysqli->options(MYSQL_OPT_READ_TIMEOUT, 3);
$mysqli->options(MYSQL_OPT_WRITE_TIMEOUT, 1);
//连接数据库
$mysqli->real_connect("localhost", "root", "123456", "discuz");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//执行查询 sleep 1秒不超时
printf("Host information: %s\n", $mysqli->host_info);
if (!($res=$mysqli->query('select sleep(1)'))) {
echo "query1 error: ". $mysqli->error ."\n";
} else {
echo "Query1: query success\n";
}
//执行查询 sleep 9秒会超时
if (!($res=$mysqli->query('select sleep(9)'))) {
echo "query2 error: ". $mysqli->error ."\n";
} else {
echo "Query2: query success\n";
}
$mysqli->close();
echo "close mysql connection\n";
作者:http://blog.csdn.net/heiyeshuwu/archive/2010/09/08/5869813.aspx
相关阅读 更多 +