php 在非对象上调用成员函数 bind_param()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4488035/
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
Call to a member function bind_param() on a non-object
提问by mcbeav
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.
我知道这个函数没有完全写在这里,但这应该不是问题。我不明白为什么我会收到这个错误。
采纳答案by oezi
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.
听起来准备调用失败(不知道为什么),所以它返回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 异常,这将告诉您准备调用失败的原因。
回答by Stefan
Well, one reason prepare()
can fail is if the sql statement sent to it is not valid in the current DB.
好吧,prepare()
失败的原因之一是发送给它的 sql 语句在当前数据库中无效。
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.
例如,如果表名不正确或查询中的一个或多个字段不存在。
回答by fvdz
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;
回答by PHP Hater
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 端,我的意思是,如果在您的查询中,您有一个表恰好具有相同的关键字名称,例如“用户”、“订单”…… ,它只是不将其识别为表,而是将其识别为关键字命令实际执行的操作,因此查询结果一团糟,准备失败。
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.
这是正确的,但是,我觉得准备有这种行为很愚蠢。
回答by Maciel Bombonato
I am trying to help other people with little experience in PHP like me.
我正在努力帮助像我这样在 PHP 方面几乎没有经验的其他人。
In my case, this error occurred because I had an SQL syntax error. The console stack trace did not show the problem.
就我而言,发生此错误是因为我遇到了 SQL 语法错误。控制台堆栈跟踪未显示问题。
When I fixed the SQL, the error was gone.
当我修复 SQL 时,错误消失了。
回答by itsites
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()”消息错误。