【发布时间】: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