我正在从 mysql 迁移到 mysqli,并且在查询中从数据库返回多行时遇到问题。
$db = new mysqli($hostname, $sql_us, $sql_us_pwd, $sql_db); // This is already connected
function db_query($db, $query, $type = 'object') {
global $db;
$result = $db->query($query);
if ($type == 'assoc') {
while($row = $result->fetch_assoc()) {
return $row;
}
} else {
while($row = $result->fetch_object()) {
return $row;
}
}
mysqli_free_result($result);
}
$query = "SELECT * FROM `users`";
$user = db_query($db, $query);
print_r($user); // This is only returning the first row of results
我显然正在尝试创建一个函数,我可以在其中查询数据库并以关联数组或对象的形式返回结果。我究竟做错了什么?