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

发布于 2024-10-08 14:10:19 字数 494 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

滥情哥ㄟ 2024-10-15 14:10:19

嗯,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.

紫瑟鸿黎 2024-10-15 14:10:19

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

听起来好像准备调用失败(不知道为什么),因此它返回 false - false 不是一个对象,所以你不能调用 bind_param () 对此。

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

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

您应该将服务器配置为返回这些 PDO 异常,这将告诉您准备调用失败的原因。

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.

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.

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.

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).

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

固执像三岁 2024-10-15 14:10:19

我也在使用 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;

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:

$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;
凉薄对峙 2024-10-15 14:10:19

看来prepare是相当愚蠢的。它并不完全依赖于MySQL端的查询,我的意思是,如果在你的查询中,你有一个表恰好具有相同的关键字名称,比如“user”,“order”,...... ,它只是不将其识别为表,而是将其识别为关键字命令实际执行的操作,因此查询变得一团糟,准备失败。

要解决这个问题很简单,您必须以“正确”的方式键入它,在表名称的两侧添加“`”。示例:

`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:

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

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

祁梦 2024-10-15 14:10:19

我正在努力帮助其他像我一样缺乏 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.

垂暮老矣 2024-10-15 14:10:19

检查数据库中用户的权限。没有“插入”权限的用户在尝试插入时也会导致“在非对象上调用成员函数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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文