【发布时间】:2015-11-01 13:21:55
【问题描述】:
我目前正在浏览一个站点并替换所有用于返回 mysql_fectch_array() 结果的函数,这些函数被放入其他地方的 while 循环中。我试图让它们以相同的格式返回相同的数据,但使用 mysqli 准备好的语句输出。我已经成功地使用下面的代码为单行结果生成了相同格式的输出。
public function get_email_settings(){
$stmt = $this->cn->stmt_init();
$stmt->prepare("SELECT * FROM email_setting WHERE user_id = ? LIMIT 1");
$stmt->bind_param("i", $this->user);
$stmt->execute();
$stmt->bind_result(
$row['email_id'],
$row['user_id'],
$row['news'],
$row['new_message'],
$row['new_friend'],
$row['rule_assent'],
$row['agreement_ready'],
$row['agreement_all_assent'],
$row['time_cap'],
$row['donations']
);
$stmt->store_result();
$stmt->fetch();
$stmt->close();
return $row;
}
但是当它返回多行时,我怎样才能让这个代码工作呢?我希望它产生与我写的相同的结果:
返回 mysql_fetch_array($result);
有可能吗?
【问题讨论】:
-
不知道你为什么用
bind_result(),试试php.net/manual/en/mysqli-result.fetch-all.php -
为什么要在查询中添加
LIMIT 1?这可以解释单行结果吗? -
谢谢,但我无法确切地弄清楚如何让 fetch_all 正常工作。我使用带有 LIMIT 1 的示例代码作为我如何使单行工作查询工作的工作示例。我只是不能让它对结果产生多于一行的查询起作用。