[英]PHP Prepared statement bind_param() error
stackoverflow 上的很多人都遇到过这个问题,但我仍然无法发现错误
这是错误:
Fatal error: Call to a member function bind_param() on boolean -
这是代码行:
$insertpost = $conn->prepare("INSERT INTO posts (title,post,user,img,date,short) VALUES(?,?,?,?,NOW(),?)");
$insertpost->bind_param("sssss",$title,$comment,$user,$url,$short);
更新:对于这个问题,我的原始答案完全不正确。 date
不是保留字,可以不加引号使用(感谢你们的教育,伙计们)。 但是,因为未加引号的保留字可能是一个常见的问题,可能会导致同样的错误,我将把它留给未来的读者,以防它有帮助( https://meta.stackexchange.com/questions/37738/when-or -你应该删除你的不正确答案)。 基本上:
检查您的列名称。 您可能有未引用的保留字。
原始答案:
您需要用反引号引用您的列名。 您的date
字段是保留字。
$insertpost = $conn->prepare("INSERT INTO posts (`title`,`post`,`user`,`img`,`date`,`short`) VALUES(?,?,?,?,NOW(),?)");
在每个可能返回错误/失败状态的mysqli
或PDO
函数之后,您必须测试这种可能性。
错误
Fatal error: Call to a member function bind_param() on boolean
说明了一切。$insertpost
是假的,因此准备由于某种原因失败,这可能是一个错误的查询,或者可能是 MYSQL 服务器已经崩溃并且无法准备语句。
所以你必须相应地编码
$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.