[PHP]MySQL的wait_timeout与pdo对象
2023-02-18 15:46:55 时间
1.查看和设置mysql的wait_timeout的值
SHOW GLOBAL VARIABLES LIKE '%timeout%';
设置wait_timeout的值
SET GLOBAL wait_timeout=10;
2.当程序中有超过10秒的执行后,再次去执行一条sql语句 , 就会报错
Warning: Error while sending QUERY packet 或者 MySQL server has gone away
3.因此我们在使用单例的PDO对象的时候,要进行时间上的判断,比如我上面的例子,超过10秒的PDO对象,要重新new一下
<?php $option=array(); $timeout=time()+10; $pdo=new PDO("mysql:host=localhost;dbname=my_test","root","xxxx",$option); var_dump($pdo); /* object(PDO)#1 (0) { } */ //此时执行了一个11秒时间的操作 sleep(11); var_dump($pdo); /* object(PDO)#1 (0) { } */ //此时需要重新new一下PDO对象,才可以继续操作,否则报错 if($timeout<time()){ $pdo=new PDO("mysql:host=localhost;dbname=my_test","root","xxxxx",$option); } $pdo->query('set names utf8'); $sth=$pdo->prepare("select * from index_test"); $sth->execute(array("id"=>2000)); $res=$sth->fetchAll(); print_r($res);
相关文章
- 打包一沓开源的 C/C++ 包管理工具送给你!
- 看看大神 Paul Graham 对如何学习编程的回答
- 喵星人教你 HTTP 状态码
- 抛却纷争,百度给开源世界带来了什么?
- 确认过眼神,看清 HTTP 协议
- 想做前端开发?推荐几个必备珍品组件库
- 高考完?入门级的开源项目带你开启编程之旅
- Flask 笔记
- MySSL HTTPS 评级 B 升 A+
- No module named MySQLdb
- SublimeText 修改文件扩展名的默认语法高亮
- chrome 错误 ERR_CACHE_READ_FAILURE
- 大数据的技术生态概述(转载)
- pyhive 连接 Hive 时错误
- Presto 常用配置及操作
- Hive 导入 parquet 格式数据
- uWSGI 踩坑记
- There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
- 认证
- SQLite向MySQL的数据库迁移