bind_param()对非对象错误

时间:2014-09-21 09:40:46

标签: php arrays mysqli prepared-statement

我有以下功能:

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

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

提前致谢。

2 个答案:

答案 0 :(得分:1)

尝试在循环中取消设置$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;
    }
}

这应该有所帮助。

答案 1 :(得分: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;
}