调用非对象上的成员函数bind_param() [duplicate]

时间:2022-06-02 19:26:57

This question already has an answer here:

这个问题已经有了答案:

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.

我知道函数并没有完全写在这里,但这不应该是问题。我不明白为什么我收到这个错误。

6 个解决方案

#1


24  

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.

正如错误消息所说,$qSelect似乎不是对象。尝试通过使用var_dump($qSelect)调试这个;之后你准备调用。还要检查getDBH()是否返回所需的内容。

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.

听起来好像prepare-call失败(不知道为什么),因此返回false - false不是对象,因此不能在该对象上调用bind_param()。

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.

编辑:您还没有给出信息,但是看起来您正在使用PHP的PDO。在这种情况下,请查看文档。

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: prepare()返回一个PDOStatement对象。如果数据库服务器无法成功地准备语句,则PDO: prepare()返回FALSE或引发PDOException(取决于错误处理)。

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

您应该配置服务器以返回这些pdo - exception,这将告诉您为什么准备调用失败。

#2


68  

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.

prepare()然后返回false。

Eg - if the table name is not correct or one or more field in the query does not exist.

如果表名不正确,或者查询中不存在一个或多个字段。

#3


17  

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:

我也在使用mysqli方法,当我在关闭第一个实例之前创建另一个mysqli实例时,也得到了相同的错误。因此,在开始同一段代码之前使用close()非常重要。例如:

$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username);
$qSelect->close();  // <--- use close before calling the same function( wich contains $DBH code) again;

#4


2  

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.

看来准备是相当愚蠢的。它并不完全依赖于MySQL端查询,我的意思是,如果在查询中,您有一个表,它恰好有一个相同的关键字的名称,比如“user”,“order”,……,它只是不承认它是一个表,而是认为关键字命令实际上是做什么的,所以查询结果是一团糟,准备工作失败了。

To fix this is simple, you have to type it in the "correct" way adding "`" in both sides of the table name. Example:

要解决这个问题很简单,您必须以“正确”的方式在表名的两边加上“'”。例子:

`user`, `order`, `...`

It's correct, yet, I find it silly from prepare to have this behavior.

这是对的,然而,我发现准备这样做是愚蠢的。

#5


1  

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.

检查数据库中用户的权限。没有“插入”权限的用户在尝试插入时,也会在非对象消息错误上引发对成员函数bind_param()的“调用”。

#6


1  

Only for try to help people without experience in PHP like me.

只是为了帮助像我这样没有PHP经验的人。

In my case this error occured because I had an error in SQL sintax. The console stack trace don't show this problem.

在我的例子中,这个错误是因为我在SQL sintax中有一个错误。控制台堆栈跟踪没有显示这个问题。

When I fixed the SQL the proccess gone ok.

当我修复SQL时,过程进行得很顺利。

#1


24  

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.

正如错误消息所说,$qSelect似乎不是对象。尝试通过使用var_dump($qSelect)调试这个;之后你准备调用。还要检查getDBH()是否返回所需的内容。

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.

听起来好像prepare-call失败(不知道为什么),因此返回false - false不是对象,因此不能在该对象上调用bind_param()。

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.

编辑:您还没有给出信息,但是看起来您正在使用PHP的PDO。在这种情况下,请查看文档。

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: prepare()返回一个PDOStatement对象。如果数据库服务器无法成功地准备语句,则PDO: prepare()返回FALSE或引发PDOException(取决于错误处理)。

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

您应该配置服务器以返回这些pdo - exception,这将告诉您为什么准备调用失败。

#2


68  

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.

prepare()然后返回false。

Eg - if the table name is not correct or one or more field in the query does not exist.

如果表名不正确,或者查询中不存在一个或多个字段。

#3


17  

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:

我也在使用mysqli方法,当我在关闭第一个实例之前创建另一个mysqli实例时,也得到了相同的错误。因此,在开始同一段代码之前使用close()非常重要。例如:

$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username);
$qSelect->close();  // <--- use close before calling the same function( wich contains $DBH code) again;

#4


2  

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.

看来准备是相当愚蠢的。它并不完全依赖于MySQL端查询,我的意思是,如果在查询中,您有一个表,它恰好有一个相同的关键字的名称,比如“user”,“order”,……,它只是不承认它是一个表,而是认为关键字命令实际上是做什么的,所以查询结果是一团糟,准备工作失败了。

To fix this is simple, you have to type it in the "correct" way adding "`" in both sides of the table name. Example:

要解决这个问题很简单,您必须以“正确”的方式在表名的两边加上“'”。例子:

`user`, `order`, `...`

It's correct, yet, I find it silly from prepare to have this behavior.

这是对的,然而,我发现准备这样做是愚蠢的。

#5


1  

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.

检查数据库中用户的权限。没有“插入”权限的用户在尝试插入时,也会在非对象消息错误上引发对成员函数bind_param()的“调用”。

#6


1  

Only for try to help people without experience in PHP like me.

只是为了帮助像我这样没有PHP经验的人。

In my case this error occured because I had an error in SQL sintax. The console stack trace don't show this problem.

在我的例子中,这个错误是因为我在SQL sintax中有一个错误。控制台堆栈跟踪没有显示这个问题。

When I fixed the SQL the proccess gone ok.

当我修复SQL时,过程进行得很顺利。