I'm trying to write a PHP-script that will fetch multiple rows from MySQL and return them as a JSONObject, the code works if I try to only fetch 1 row but if I try to get more than one at a time the return string is empty.
$i = mysql_query("select * from database where id = '$v1'", $con);
$temp = 2;
while($row = mysql_fetch_assoc($i)) {
$r[$temp] = $row;
//$temp = $temp +1;
}
If I write the code like this it returns what I expect it to, but if I remove the // from the second row in the while loop it will return nothing. Can anyone explain why this is and what I should do to solve it?
mysql_*functions anymore because they're deprecated and get removed with one of the next versions of PHP. UsePDOormysqliinstead. Then you should initialize your array$rwith$r = array();. You can do++$item;to increase a counting variable. And you should get all results in the$rvariable afterwards.$tempfrom 2?