php (致命错误:在非对象上调用成员函数 bind_param())
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18921670/
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
(Fatal error: Call to a member function bind_param() on a non-object)
提问by Jonas Franz
I get an error with this text:(sorry for my bad english I am from germany!)
我收到此文本的错误:(对不起,我的英语不好,我来自德国!)
Error:Fatal error: Call to a member function bind_param() on a non-object in /users/ftf/www/ccache.php on line 44
错误:Fatal error: Call to a member function bind_param() on a non-object in /users/ftf/www/ccache.php on line 44
A part of the Code from ccache.php
来自 ccache.php 的部分代码
// Neues Datenbank-Objekt erzeugen
$db = @new mysqli( 'localhost', 'ftf', '***', 'ftf' );
// Pruefen ob die Datenbankverbindung hergestellt werden konnte
if (mysqli_connect_errno() == 0)
{
$sql = "INSERT INTO cache
('name', 'user', 'veroefentlichung', 'beschreibung', 'FTFcode', 'STFcode', 'TTFcode', 'type', 'lat', 'lon', 'address', 'link')
VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')";
$eintrag = $db->stmt_init();
$eintrag = $db->prepare( $sql );
$eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44
$eintrag->execute();
// Pruefen ob der Eintrag efolgreich war
if ($eintrag->affected_rows == 1)
{
echo 'Der neue Eintrage wurde hinzugefügt.';
}
else
{
echo 'Der Eintrag konnte nicht hinzugefügt werden.';
}
}
采纳答案by Robert Iulian
$eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44
You need to the define the type of parameters as this:
您需要将参数类型定义为:
$eintrag->bind_param("ssssssiiss", $titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44
s - string i - int Also check documentation: http://php.net/manual/en/mysqli-stmt.bind-param.php
s - 字符串 i - int 还要检查文档:http: //php.net/manual/en/mysqli-stmt.bind-param.php
回答by Sammitch
Check your return values!
检查您的返回值!
Bad: $eintrag = $db->prepare( $sql )
坏的: $eintrag = $db->prepare( $sql )
Good:
好的:
if( ! $eintrag = $db->prepare( $sql ) ) {
echo 'Error: ' . $db->error;
return false; // throw exception, die(), exit, whatever...
} else {
// the rest of your code
}
The same goes for $eintrag->execute();
.
也是如此$eintrag->execute();
。
Also, the problem is probably the fact that you're wrapping your ?
placeholders in quotes. Don't do that. MySQLi does that for you.
此外,问题可能在于您将?
占位符用引号括起来。不要那样做。MySQLi 为您做到了这一点。
回答by bdf
The error message Call to a member function bind_param() on a non-object
... means that you haven't properly instantiated the object $eintrag
before calling bind_params()
on it.
错误消息Call to a member function bind_param() on a non-object
... 表示您$eintrag
在调用对象之前没有正确实例化该对象bind_params()
。
It could be because you are trying to instantiate $eintrag
from $db
, and it is your line $db = @new mysqli( 'localhost', 'ftf', '***', 'ftf' );
which is actually failing.
这可能是因为你试图实例$eintrag
从$db
,这是你的线$db = @new mysqli( 'localhost', 'ftf', '***', 'ftf' );
这实际上是失败的。
Try removing the '@', then you can at least read any errors/notices/warnings:
尝试删除“@”,然后您至少可以阅读任何错误/通知/警告:
$db = new mysqli( 'localhost', 'ftf', '***', 'ftf' );
$db = new mysqli( 'localhost', 'ftf', '***', 'ftf' );
回答by webquestion
Change the code to:
将代码更改为:
$sql = "INSERT INTO cache
(name, user, veroefentlichung, beschreibung, FTFcode, STFcode, TTFcode, type, lat, lon, 'address', 'link')
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
(i.e. delete the quotes)
(即删除引号)