php 在非对象 MySQLi 上调用成员函数 bind_param()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17768200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:08  来源:igfitidea点击:

Call to a member function bind_param() on a non-object MySQLi

phpmysqli

提问by Austen

Okay, so I'm trying to update a blog entry, and I'm getting Call to a member function bind_param() on a non-object when I try to run the script. I have done extensive research to see if I could fix it myself, but I must be missing something.

好的,所以我正在尝试更新博客条目,当我尝试运行脚本时,我正在调用非对象上的成员函数 bind_param()。我已经做了广泛的研究,看看我是否可以自己修复它,但我一定是遗漏了一些东西。

<?php
$stmt = $mysqli->prepare("UPDATE blogentries SET 
  headline = ?, 
   image = ?, 
   caption = ?,  
   article = ?
    WHERE id = ?");
$stmt->bind_param('ssssi',
   $_POST['headline'],
   $_POST['image'],
   $_POST['caption'],
   $_POST['article'],
   $_POST['id']);
$stmt->execute(); 
$stmt->close();

?>

Thanks in advance,

提前致谢,

Austen

奥斯汀

Update: Here's the db connect

更新:这是数据库连接

I added the extra $mysqli connection for debugging purposes, and the error occurs even without it.

我添加了额外的 $mysqli 连接用于调试目的,即使没有它也会发生错误。

回答by bitWorking

$stmtis probably false.

$stmt大概是false

if ($stmt = $mysqli->prepare(...)) {
    $stmt->bind_param(...);
    ...
}
else {
    printf("Errormessage: %s\n", $mysqli->error);
}

回答by Daniel W.

I solved that by testing the queries manually. It turned out to be a matter of putting each field name between back ticks and removing any quotes against the parameters labeled with question marks inside the query.

我通过手动测试查询解决了这个问题。结果证明是将每个字段名称放在反引号之间并删除针对查询中标有问号的参数的任何引号。

回答by FullStackEngineer

If all the connections to the database are correct, try looking for the syntax in the query. For me, I was performing a join and did not specify the "TABLENAME" in the where clause-field for which both the tables have a column.

如果与数据库的所有连接都正确,请尝试在查询中查找语法。对我来说,我正在执行连接并且没有在两个表都有一列的 where 子句字段中指定“TABLENAME”。