|
54樓 巨大八爪鱼
2015-5-31 08:58
$sql = "INSERT INTO `timetest` VALUES (null, NOW())"; $successful = $dbh->exec($sql); if ($successful) { echo "插入記錄成功"; }
|
|
55樓 巨大八爪鱼
2015-5-31 08:59
回復:54樓 這段代碼如果改寫成mysql原來的代碼,就很複雜,除了執行mysql_query($sql),還需要執行mysql_affected_rows() 因為對於插入、刪除、修改的SQL語句,mysql_query始終返回true
|
|
56樓 巨大八爪鱼
2015-5-31 09:03
PDO::query() returns a PDOStatement object, or FALSE
on failure. query主要用於SELECT語句,返回PDOStatement對象
|
|
57樓 巨大八爪鱼
2015-5-31 09:14
$content = join(", ", PDO::getAvailableDrivers()); if (!empty($content)) { $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())"; $stmt = $dbh->prepare($sql); $stmt->bindParam(":content", $content); $successful = $stmt->execute(); if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}條記錄。"; } else { echo "插入記錄失敗!"; } } else { echo "沒有內容"; }
|
|
58樓 巨大八爪鱼
2015-5-31 09:17
$content = join(", ", PDO::getAvailableDrivers()); if (!empty($content)) { $sql = "SELECT * FROM Contents WHERE Content = ?"; $stmt = $dbh->prepare($sql); $stmt->execute(array($content)); if ($stmt->rowCount()){ echo "數據表中已經有相同的內容了。"; } else { $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())"; $stmt = $dbh->prepare($sql); $stmt->bindParam(":content", $content); $successful = $stmt->execute(); if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}條記錄。"; } else { echo "插入記錄失敗!"; } } } else { echo "沒有內容"; }
|
|
59樓 巨大八爪鱼
2015-5-31 09:19
bindValue的用法 $sql = "SELECT * FROM Contents WHERE Content = ?"; $stmt = $dbh->prepare($sql); $stmt->bindValue(1, $content); $stmt->execute();
|
|
60樓 巨大八爪鱼
2015-5-31 09:24
對於$stmt->queryString這個屬性,輸出內容如下: INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW()) 也就是語句中仍然沒有包含具體的值。
if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}條記錄。<br>"; echo $stmt->queryString; } else { echo "插入記錄失敗!"; }
|
|
61樓 巨大八爪鱼
2015-5-31 09:28
$dbh->beginTransaction(); $content = "數據表中已經有相同的內容了。"; if (!empty($content)) { $sql = "SELECT * FROM Contents WHERE Content = ?"; $stmt = $dbh->prepare($sql); $stmt->execute(array($content)); /*$stmt->bindValue(1, $content); $stmt->execute();*/ if ($stmt->rowCount()){ echo "數據表中已經有相同的內容了。"; } else { $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())"; $stmt = $dbh->prepare($sql); $stmt->bindParam(":content", $content); $successful = $stmt->execute(); $dbh->rollBack(); echo "rollback"; /*if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}條記錄。"; //echo $stmt->queryString; } else { echo "插入記錄失敗!"; }*/ } } else { echo "沒有內容"; }
|
|
62樓 巨大八爪鱼
2015-5-31 09:30
發現一個嚴重的問題,雖然rollback了,但是AUTO_INCREMENT卻增加了!!!!
|
|
63樓 巨大八爪鱼
2015-5-31 09:31
|