当我执行下面的代码时,我总是收到一个超时。这里是确切的错误:写入mysql时PHP超时
Warning: PDO::__construct() [pdo.--construct]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in D:\xampp\htdocs\logansarchive\admin\articlework.php on line 16 Fatal error: Maximum execution time of 60 seconds exceeded in D:\xampp\htdocs\logansarchive\admin\articlework.php on line 0
我觉得这是值得一提的是,当这个没有工作,当它没有,什么也没有我的Web服务器上或在此代码改变的时间之间。
下面是做数据库的工作代码:
<?php
$action = $_REQUEST["action"];
$target = $_REQUEST["target"];
$srctitle = $_POST["srctitle"];
$title = $_POST["article_title"];
$cat = $_POST["article_cat"];
$content = $_POST["article_content"];
// Set database server access variables:
$host = "localhost";
$user = "root";
$pass = "root";
$db = "logansarchive";
// Open connection
$dbh = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass);
$date = date('Y-m-d H:i:s');
switch ($action) {
case "Edit":
$query = $dbh->prepare("UPDATE Articles ".
"SET ArticleTitle = :title, Category = :cat, ArticleDate = :date, ArticleContent = :content ".
"WHERE ArticleTitle = :srctitle");
$query->bindParam(':title', $title);
$query->bindParam(':cat', $cat);
$query->bindParam(':date', $date);
$query->bindParam(':content', $content);
$query->bindParam(':srctitle', $srctitle);
$query->execute();
break;
case "New":
$query = $dbh->prepare("INSERT INTO Articles(Category, ArticleDate, ArticleTitle, ArticleContent) ".
"VALUES(:cat, :date, :title, :content)");
$query->bindParam(':cat', $cat);
$query->bindParam(':date', $date);
$query->bindParam(':title', $title);
$query->bindParam(':content', $content);
$query->execute();
break;
case "Delete":
if ($target != "") {
$query = $dbh->prepare("UPDATE Articles ".
"SET DeletedYN = :del ".
"WHERE ArticleTitle = :title");
$query->bindValue(':del', "Yes");
$query->bindParam(':title', $target);
$query->execute();
}
else {
header("Location: index.php?result=failed");
}
break;
}
header("Location: index.php?result=success");
?>
编辑1:我刚刚意识到的东西,可以缩小问题的原因。 上面的代码是我使用PDO的网站的唯一部分。所有其余的使用mysql_ *,它一切正常。这使我相信问题在于PDO。
看来,PDO在连接死亡数据库是否被重载,是否被其他一些查询占用了所有的连接槽?是否重复检查了数据库的连接参数? – Sheepy
通过控制台登录到mysql服务器并发出'show processlist;'命令。你看到一些长寿命的连接,可能你的数据库被超负荷了。如果有一些'锁定'连接,yuor数据库处于死锁状态,请尝试从最旧的连接开始,终止锁定连接 – J0HN
我不这么认为你的代码导致超时,首先你需要检查为什么连接失败? –