【问题标题】:bind_param() on a non object errorbind_param() 对非对象错误
【发布时间】:2014-11-15 11:06:10
【问题描述】:

我有以下功能:

public function detail($detail, $table, $column, $value) {
        if(is_array($detail)) {
            $data = array();

            foreach($detail as $key) {
                $stmt = $this->mysqli->prepare("SELECT `$key` FROM `$table` WHERE `$column` = ?");
                if(is_numeric($value)) {
                    $stmt->bind_param('i', $value);
                } else {
                    $stmt->bind_param('s', $value);
                }
                $stmt->execute();
                $stmt->bind_result($detail);
                $stmt->fetch();
                $data[] = $detail;
            }

            return $data;
        } else {
            $stmt = $this->mysqli->prepare("SELECT `$detail` FROM `$table` WHERE `$column` = ?");

            if(is_numeric($value)) {
               $stmt->bind_param('i', $value);
            } else {
                $stmt->bind_param('s', $value);
            }
            $stmt->execute();
            $stmt->bind_result($detail);
            $stmt->fetch();

            return $detail;
        }
    }

在我使用数组之前,此函数运行良好。使用这个函数的方法是这样的:$db->detail('username', 'users', 'id', 1) 这将返回 id 为 1 的用户的用户名(这工作正常)。就像我说的那样,当我使用数组时问题就开始了,例如:

$details = array('username', 'active', 'registered');
$details = $db->detail($details, 'users', 'id', 1);
print_r($details);

错误指向if(is_array()) 中的$stmt->bind_param('i', $value);。我已经尝试过bind_param on a non-object 的答案,但这对我没有帮助;我仍然得到同样的错误。 我希望有人知道如何为我修复 Fatal error: Call to a member function bind_param() on a non-object 错误。

提前致谢。

【问题讨论】:

  • 很可能是因为prepare()失败了,所以你绑定的时候没有成功
  • 可能是因为您正在覆盖变量 $detail 导致下一次迭代中的准备失败

标签: php arrays mysqli prepared-statement


【解决方案1】:

我认为在不使用循环的情况下准备查询的有效方法是在数组中内爆值,而不是循环和准备查询语句。 例如:- 如果查询是

SELECT `username`,`active`,`register` FROM users WHERE ID = 1 //username,active and register can be used in single prepare statement by imploding the array


if(is_array($detail)) {
    $data = array();

        $stmt = $this->mysqli->prepare("SELECT ".implode(", ",$detail)." FROM `$table` WHERE `$column` = ?");
        if(is_numeric($value)) {
            $stmt->bind_param('i', $value);
        } else {
            $stmt->bind_param('s', $value);
        }
        $stmt->execute();
        $stmt->bind_result($detail);
        $stmt->fetch();
        $data[] = $detail;
        $stmt = null;

    return $data;
}

【讨论】:

  • 这行不通的原因有两个: 1. 你必须从查询中删除` ` 否则查询将不正确; 2. 在bind_result 语句中,您必须分别传递每一列。
【解决方案2】:

尝试在循环中取消设置$stmt 变量:

public function detail($detail, $table, $column, $value) {
    if(is_array($detail)) {
        $data = array();

        foreach($detail as $key) {
            $stmt = $this->mysqli->prepare("SELECT `$key` FROM `$table` WHERE `$column` = ?");
            if(is_numeric($value)) {
                $stmt->bind_param('i', $value);
            } else {
                $stmt->bind_param('s', $value);
            }
            $stmt->execute();
            $stmt->bind_result($detail);
            $stmt->fetch();
            $data[] = $detail;
            $stmt = null;
        }

        return $data;
    } else {
        $stmt = $this->mysqli->prepare("SELECT `$detail` FROM `$table` WHERE `$column` = ?");

        if(is_numeric($value)) {
            $stmt->bind_param('i', $value);
        } else {
            $stmt->bind_param('s', $value);
        }
        $stmt->execute();
        $stmt->bind_result($detail);
        $stmt->fetch();

        return $detail;
    }
}

这应该会有所帮助。

【讨论】:

  • 这很奇怪,因为它在我的服务器上工作正常。您是否在foreach 正文的末尾添加了$stmt = null; 行?
  • 我注意到我在数组的某处打错了字,这就是为什么我仍然遇到同样的错误,但现在它可以工作了。太棒了!
猜你喜欢
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
相关资源
最近更新 更多