在非对象上调用成员函数 bind_param() [英] Call to a member function bind_param() on a non-object

查看:35
本文介绍了在非对象上调用成员函数 bind_param()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在这个准备好的语句中绑定一个变量,但我一直收到错误:

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.

推荐答案

正如错误消息所说,$qSelect 似乎不是一个对象.尝试在准备调用后立即使用 var_dump($qSelect); 进行调试.还要检查 getDBH() 是否返回您需要的内容.

as the error-message says, $qSelect seems to be not an object. try to debug this by using var_dump($qSelect); right after your prepare-call. also check if getDBH() returns what you need.

听起来像准备调用失败(不知道为什么),所以它返回 false - false 不是一个对象,所以你不能调用 <代码>bind_param() 关于那个.

sounds like the prepare-call fails (don't know why) and so it returns false - false is not an object, so you can't call bind_param() on that.

您没有提供信息,但看起来您正在使用 PHP 的 PDO.在这种情况下,请查看文档.

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.

如果数据库服务器成功准备语句,PDO::prepare()返回一个 PDOStatement 对象.如果数据库服务器无法成功准备语句,PDO::prepare()返回 FALSE 或发出 PDOException(取决于错误处理).

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

您应该配置您的服务器以返回那些 PDO-Exceptions,这将告诉您准备调用失败的原因.

You should configure your server to return those PDO-Exceptions, which would tell you why the prepare call fails.

这篇关于在非对象上调用成员函数 bind_param()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆