(致命错误:在非对象上调用成员函数bind_param())

Jon*_*anz 4 php mysqli sqlbindparameter

我在这篇文章中收到错误:(抱歉我的英文不好,我来自德国!)

错误:Fatal error: Call to a member function bind_param() on a non-object in /users/ftf/www/ccache.php on line 44

来自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.';
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sam*_*tch 5

检查您的退货价值!

坏: $eintrag = $db->prepare( $sql )

好:

if( ! $eintrag = $db->prepare( $sql ) ) {
  echo 'Error: ' . $db->error;
  return false; // throw exception, die(), exit, whatever...
} else {
  // the rest of your code
}
Run Code Online (Sandbox Code Playgroud)

同样的道理$eintrag->execute();.

此外,问题可能在于您将?占位符包装在引号中.不要那样做.MySQLi为您做到了这一点.