简体   繁体   English

PHP 准备语句 bind_param() 错误

[英]PHP Prepared statement bind_param() error

Many people on stackoverflow has had this problem, but i still cannot spot the mistake stackoverflow 上的很多人都遇到过这个问题,但我仍然无法发现错误

This is the error:这是错误:

Fatal error: Call to a member function bind_param() on boolean -

This is the lines of code:这是代码行:

$insertpost = $conn->prepare("INSERT INTO posts (title,post,user,img,date,short) VALUES(?,?,?,?,NOW(),?)");
$insertpost->bind_param("sssss",$title,$comment,$user,$url,$short);

UPDATE: My original answer was starkly incorrect for this question.更新:对于这个问题,我的原始答案完全不正确。 date is not a reserved word and can be used without quoting (thanks for the schooling, guys). date不是保留字,可以不加引号使用(感谢你们的教育,伙计们)。 However, because unquoted reserved words can be a common issue which could result in the same error, I'm leaving this up for future readers in case it helps ( https://meta.stackexchange.com/questions/37738/when-or-should-you-delete-your-incorrect-answer ).但是,因为未加引号的保留字可能是一个常见的问题,可能会导致同样的错误,我将把它留给未来的读者,以防它有帮助( https://meta.stackexchange.com/questions/37738/when-or -你应该删除你的不正确答案)。 Basically:基本上:

Check your column names.检查您的列名称。 You may have unquoted reserved words.您可能有未引用的保留字。

https://dev.mysql.com/doc/refman/5.5/en/keywords.html https://dev.mysql.com/doc/refman/5.5/en/keywords.html


ORIGINAL ANSWER:原始答案:

You need to quote your column names with backticks.您需要用反引号引用您的列名。 Your date field is a reserved word.您的date字段是保留字。

$insertpost = $conn->prepare("INSERT INTO posts (`title`,`post`,`user`,`img`,`date`,`short`) VALUES(?,?,?,?,NOW(),?)");

https://dev.mysql.com/doc/refman/5.5/en/keywords.html https://dev.mysql.com/doc/refman/5.5/en/keywords.html

After every mysqli or PDO function that COULD return a bad/failed status you must test for that possibility.在每个可能返回错误/失败状态的mysqliPDO函数之后,您必须测试这种可能性。

The error Fatal error: Call to a member function bind_param() on boolean says it all.错误Fatal error: Call to a member function bind_param() on boolean说明了一切。 $insertpost is false so the prepare failed for SOME reason, it could be a bad query or it could be that the MYSQL Server has crashed and cannot prepare the statement. $insertpost是假的,因此准备由于某种原因失败,这可能是一个错误的查询,或者可能是 MYSQL 服务器已经崩溃并且无法准备语句。

So you must code accordingly所以你必须相应地编码

$insertpost = $conn->prepare("INSERT INTO posts 
                                    (title,post,user,img,date,short) 
                              VALUES(?,?,?,?,NOW(),?)");

if ( $insertpost === FALSE ) {
    // prepare failed for some reason, lets see why
    echo $conn->error;
    exit;
}
$insertpost->bind_param("sssss",$title,$comment,$user,$url,$short);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM