调用非对象上的成员函数bind_param()
我试图在此准备好的语句中绑定一个变量,但我不断收到错误:
Call to a member function bind_param() on a non-object
调用该函数,并将变量传递给它。当我更改函数以仅回显变量时,变量可以很好地打印在页面上,但是如果我尝试将其绑定到此处,我会收到错误。有人可以帮忙吗?
//CALL FROM PAGE ONE
check($username);
//FUNCTION ON PAGE 2
function check($username){
$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username);
}
我知道这个函数没有完全写在这里,但这不应该是一个问题。我不明白为什么我会收到此错误。
I am trying to bind a variable in this prepared statement, but i keep receiving the error:
Call to a member function bind_param() on a non-object
The function is called, and variables are passed to it. When i change the function to just echo the variable, the variable prints on the page fine, but if i try to bind it here i receive the error. can anyone help?
//CALL FROM PAGE ONE
check($username);
//FUNCTION ON PAGE 2
function check($username){
$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username);
}
i know the function is not completely written here, but that shouldn't be a problem. I don't understand why i am receiving this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
嗯,
prepare()
失败的原因之一是发送给它的 sql 语句在当前数据库中无效。prepare()
将返回 false。例如 - 如果表名不正确或者查询中的一个或多个字段不存在。
Well, one reason
prepare()
can fail is if the sql statement sent to it is not valid in the current DB.prepare()
will then return false.Eg - if the table name is not correct or one or more field in the query does not exist.
正如错误消息所述,
$qSelect
似乎不是一个对象。尝试在准备调用之后使用var_dump($qSelect);
来调试它。还要检查 getDBH() 是否返回您需要的内容。听起来好像准备调用失败(不知道为什么),因此它返回
false
-false
不是一个对象,所以你不能调用bind_param ()
对此。编辑:您还没有提供信息,但看起来您正在使用 PHP 的 PDO。在这种情况下,请查看文档。
您应该将服务器配置为返回这些 PDO 异常,这将告诉您准备调用失败的原因。
as the error-message says,
$qSelect
seems to be not an object. try to debug this by usingvar_dump($qSelect);
right after your prepare-call. also check ifgetDBH()
returns what you need.sounds like the prepare-call fails (don't know why) and so it returns
false
-false
is not an object, so you can't callbind_param()
on that.EDIT: you havn't given the info, but it looks like you're using PHP's PDO. In that case, take a look at the documentation.
You should configure your server to return those PDO-Exceptions, which would tell you why the prepare call fails.
我也在使用 mysqli 方法,并且在关闭第一个实例之前创建另一个 mysqli 实例时遇到了相同的错误。因此,在启动同一段代码之前使用
close()
非常重要。例如:i'm using the mysqli approach as well and got the same error when I created another instance of mysqli before closing the first instance. So its important to use
close()
before starting the same piece of code. For example:看来prepare是相当愚蠢的。它并不完全依赖于MySQL端的查询,我的意思是,如果在你的查询中,你有一个表恰好具有相同的关键字名称,比如“user”,“order”,...... ,它只是不将其识别为表,而是将其识别为关键字命令实际执行的操作,因此查询变得一团糟,准备失败。
要解决这个问题很简单,您必须以“正确”的方式键入它,在表名称的两侧添加“`”。示例:
这是正确的,但是,我发现准备进行这种行为很愚蠢。
It appears that prepare is quite dumb. It doesn't rely query entirely into the MySQL side, by this, I mean, if in your query, you have a table that happens to have the same name of a keyword, say "user", "order", ..., it just doesn't recognize it as a table, but rather as what the keyword commands actually do, so the query turns out to be a mess and the prepare just fail.
To fix this is simple, you have to type it in the "correct" way adding "`" in both sides of the table name. Example:
It's correct, yet, I find it silly from prepare to have this behavior.
我正在努力帮助其他像我一样缺乏 PHP 经验的人。
就我而言,发生此错误是因为 SQL 语法错误。控制台堆栈跟踪没有显示问题。
当我修复 SQL 时,错误就消失了。
I am trying to help other people with little experience in PHP like me.
In my case, this error occurred because I had an SQL syntax error. The console stack trace did not show the problem.
When I fixed the SQL, the error was gone.
检查数据库中用户的权限。没有“插入”权限的用户在尝试插入时也会导致“在非对象上调用成员函数bind_param()”消息错误。
Check the permissions of the user in database. User without "insert" permission causes "Call to a member function bind_param() on a non-object" message error too, when trying to insert.