2010-03-28 59 views
3

我正在使用mysqli_stmt_bind_param()创建一个INSERT语句。出于某种原因,我收到错误。我使用mysqli_error()来查看错误消息,但它不是特别有用。返回由mysqli_stmt_bind_param创建的语句

有没有办法看到什么查询实际上正在执行?

所产生的误差:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,date,expdate,mintix,maxtix,contactname,contactemail,contactphone) VALUES (?' at line 1

+3

请告诉我们你的代码。否则很难帮助你。 – 2010-03-28 00:13:00

+0

除了byronh评论之外,您还应该能够将查询语句存储在变量中,然后{query,echo,save}查询语句和错误代码。 – bdl 2010-03-28 00:14:33

+1

@bdl我无法找到一种方法将准备好的语句转换回字符串(我认为bigmac所要求的是什么)。 – 2010-03-28 00:15:45

回答

2

由mysqli_prepare()创建的准备语句是服务器端准备好的语句。
当你执行这样一个准备好的语句时,只传输语句ID和参数,而不是某些查询字符串,就像你用实际参数(客户端,即你的PHP脚本)替换占位符一样。
但是你可以看到的结果在MySQL服务器的通用日志,看到Prepared Statement Logging

编辑:你的情况,因为desc是保留关键字声明的准备失败。
有关关键字的列表以及如何使用它们作为标识符(如果需要的话)看到http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

$q = ' 
    INSERT INTO 
    `event` 
    (
     `cityid`, `name`, `desc`, `date`, 
     `expdate`, `mintix`, `maxtix`, 
     `contactname`, `contactemail`, `contactphone` 
    ) 
    VALUES 
    (
     ?,?,?,?, 
     ?,?,?, 
     ?,?,? 
    ) 
'; 

if (false===($stmt=mysqli_prepare($dblink, $q))) { 
    /* 
    in production-code you might not want to reveal 
    the error string to each and every user 
    ...but for this example and for debugging purposes: 
    */ 
    die('mysqli_prepare failed: '.htmlspecialchars(mysqli_error($dblink))); 
} 

$rc = mysqli_stmt_bind_param(
    $stmt, 
    "issssiisss", 
    $city,$name,$desc,$date, 
    $expdate,$mintix,$maxtix, 
    $contactname,$contactemail,$contactphone 
); 
if (false===$rc) { 
    die('mysqli_stmt_bind_param failed: '.htmlspecialchars(mysqli_stmt_error($stmt))); 
} 


if (false===mysqli_stmt_execute($stmt)) { 
    die('mysqli_stmt_execute failed: '.htmlspecialchars(mysqli_stmt_error($stmt))); 
} 

mysqli_stmt_close($stmt); 
+0

啊,所以*这就是为什么mysql和PDO都不能正确支持这一点。谢谢。 – 2010-03-28 17:06:22

+0

PDO可以同时执行服务器端和客户端(模拟)预准备语句(PDO :: ATTR_EMULATE_PREPARES)。在后一种情况下,尽管它看起来没有被公开,但它可能会打印查询字符串。 – VolkerK 2010-03-28 17:47:36

+0

我以为我以前用过desc作为列名,但我错了。我很惊讶phpMyAdmin让我使用它。我只是试图改变它,phpMyAdmin显示一个错误。它仍然设法改变它,现在一切正常。 感谢所有这些方法来捕捉错误。这真的很有用。没有意识到所有这些都是可能的。 – burger 2010-03-28 18:03:50