PDO未捕获的例外情况

时间:2010-10-26 19:53:19

标签: php sql mysql pdo mysql-error-1065

执行以下代码时会出现此错误:

Fatal error: Uncaught exception 'PDOException' with message 
  'SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty'
  in /home/content/63/6563663/html/inventory/pg.php:20
Stack trace:
  #0 /home/content/63/6563663/html/inventory/pg.php(20): PDOStatement->execute()
  #1 {main} thrown in /home/content/63/6563663/html/inventory/pg.php on line 20

<?php

$u=$_GET["u"];


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$amount = isset($_POST['amount']) ? $_POST['amount'] : null;
if (null != $amount) {

$user = 'username';
$pass = 'password';
$pdo = new PDO('mysql:host=localhost', $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
session_start();
$tablename = $_SESSION['MM_Username'];
$query = sprintf("UPDATE `%s` SET `stock` = :amount WHERE `itemname` = :u ", $tablename);
$stmt = $pdo->prepare($query);
$stmt->bindParam('amount', $amount);
$stmt->execute();
}
}

?>

正常但现在出现此错误。我很抱歉,因为我是PDO的新手。

Fatal error: Uncaught exception 'PDOException' with message
  'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens'
  in /home/content/63/6563663/html/inventory/pg.php:20
Stack trace:
  #0 /home/content/63/6563663/html/inventory/pg.php(20): PDOStatement->execute()
  #1 {main} thrown in /home/content/63/6563663/html/inventory/pg.php on line 20

2 个答案:

答案 0 :(得分:2)

您应该熟悉trycatch Exceptions

答案 1 :(得分:1)

嗯,错误信息非常清楚:

  

1065查询为空

您正在使用不存在的变量$UpdateQuery。你可能想说

 $stmt = $pdo->prepare($query);