Reputation: 167
I was working on a sql query which gives random rows from table my aim is to get random ordered result set at each time and it is used with volley in android and some times it gives proper results and it works fine and some times it does not works it will not give json result but still there will be values like this "mysqli_result Object ( [current_field] => 0 [field_count] => 8 [lengths] => [num_rows] => 10 [type] => 0 )" is that the problem with rand() ? with out rand it works fine but i need random rows from table i need a specific amount of rows which chosen randomly is there any other way to do this?
<?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));
?>
Upvotes: 1
Views: 45
Reputation: 167
The problem was with my character encoding it was UTF-8 and i changed it to 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));
?>
Upvotes: 1