gpt4 book ai didi

php - SQL Rand() 给出结果集但无法获取结果

转载 作者:行者123 更新时间:2023-11-28 23:10:39 24 4
gpt4 key购买 nike

我正在研究一个 sql 查询,该查询从表中提供随机行我的目标是每次都获得随机排序的结果集,它在 android 中与 volley 一起使用,有时它会给出正确的结果并且它工作正常有时它不起作用 它不会给出 json 结果,但仍然会有像这样的值“mysqli_result Object ( [current_field] => 0 [field_count] => 8 [lengths] => [num_rows] => 10 [type] => 0 )"是 rand() 的问题吗?没有 rand 它工作正常但我需要表中的随机行我需要随机选择的特定数量的行有没有其他方法可以做到这一点?

<?php
include_once("config.php");
$r = mysqli_query($db,"Select * from qs ORDER BY RAND()
LIMIT 10");
print_r($r);
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,array(
"id"=>$row[0],
"question"=>$row[1],
"option1"=>$row[2],
"option2"=>$row[3],
"option3"=>$row[4],
"option4"=>$row[5],
"answer"=>$row[6]

)
);
}
echo json_encode(array("result"=>$result));
?>

最佳答案

问题出在我的字符编码上,它是 UTF-8,我将其更改为 UTF8$db->set_charset("utf8");

<?php

include_once("config.php");
$db->set_charset("utf8");
$r = mysqli_query($db,"Select * from qs ORDER BY RAND() LIMIT 10");
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,array(
"id"=>$row[0],
"question"=>$row[1],
"option1"=>$row[2],
"option2"=>$row[3],
"option3"=>$row[4],
"option4"=>$row[5],
"answer"=>$row[6]

)
);
}
echo json_encode(array("result"=>$result));
?>

关于php - SQL Rand() 给出结果集但无法获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46120572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com