mysqli准备更新查询失败

时间:2013-12-02 15:51:26

标签: mysqli prepare

以下代码在准备时失败:

$query = "UPDATE `table` SET `col` = ? WHERE `id` = ?";
if (!($stmt = $mysqli->prepare($query))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
//^^^ PREPARE fails
$stmt->bind_param("si", $val,$id);
if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

我明白了:警告:mysqli :: prepare()[mysqli.prepare]:无法获取mysqli ... 我在上面的代码中做错了什么? 我检查了语法文档,但如果我尝试将查询作为sql传递,我会收到语法错误。是否有一个很好的mysqli语法错误调试工具?

1 个答案:

答案 0 :(得分:-1)

您可以执行以下操作。

try{
$dbh = new PDO('mysql:host=localhost;dbname=database_name', $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$query = $dbh->prepare("UPDATE table SET col= ? WHERE id= ?");
$query->bindParam(1, $val);
$query->bindParam(2, $id);
$query->execute();

if (!$query)
 {
  echo "Execution failed";
 }
$query = null;
$dbh = null;
}catch (Exception $ex){
 echo $ex;
}

希望这对你有用。