當我執行下面的代碼時,我總是收到一個超時。這裏是確切的錯誤:寫入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
我不這麼認爲你的代碼導致超時,首先你需要檢查爲什麼連接失敗? –