【问题标题】:(Fatal error: Call to a member function bind_param() on a non-object)(致命错误:在非对象上调用成员函数 bind_param())
【发布时间】:2013-09-20 16:34:41
【问题描述】:

我收到此文本的错误:(对不起,我来自德国的英语不好!)

错误: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.';
        }
    }

【问题讨论】:

标签: php mysqli sqlbindparameter


【解决方案1】:

检查您的返回值!

错误:$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
}

$eintrag->execute(); 也是如此。

此外,问题可能在于您将 ? 占位符用引号括起来。不要那样做。 MySQLi 会为您做到这一点。

【讨论】:

  • 什么也没发生。错误在第 44 行:$eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44
  • 那么是你的prepare() 失败了。 if( ! $eintrag = $db->prepare( $sql ) ) { 其余都一样。
  • @user2792899 更新答案代码,添加错误的可能原因。
【解决方案2】:
  $eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon,       $shortdesc, $genlink); // line 44

你需要这样定义参数的类型:

$eintrag->bind_param("ssssssiiss", $titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44

s - 字符串 我 - 整数 另请查看文档:http://php.net/manual/en/mysqli-stmt.bind-param.php

【讨论】:

    【解决方案3】:

    错误消息Call to a member function bind_param() on a non-object... 表示您在调用bind_params() 之前尚未正确实例化对象$eintrag

    这可能是因为您试图从$db 实例化$eintrag,而实际上是您的行$db = @new mysqli( 'localhost', 'ftf', '***', 'ftf' ); 失败了。

    尝试删除“@”,然后您至少可以阅读任何错误/通知/警告:

    $db = new mysqli( 'localhost', 'ftf', '***', 'ftf' );

    【讨论】:

    • 还有:Fatal error: Call to a member function bind_param () on a non-object in / users / ftf / www / ccache.php on line 44
    • 可能是您的线路$eintrag = $db->stmt_init();...我不确定您是否需要该线路。检查示例 #1 php.net/manual/en/mysqli-stmt.bind-param.php
    【解决方案4】:

    将代码改为:

    $sql = "INSERT INTO cache
            (name, user, veroefentlichung, beschreibung, FTFcode, STFcode, TTFcode, type, lat, lon, 'address', 'link')
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    

    (即删除引号)

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多