How to make a mysql query a PHP Native Array
Apr 17th, 2008 by T.Bogard
Hello everyone
surely you have the problem how to convert a mysql query in a PHP array using an X * Y dimentional table, ideal if you are working with JSON and need to convert it.
it’s really easy to do, look
$recordset = mysql_query($query, $database) or die(mysql_error()); do{ for($x=0;$x<mysql_num_fields($recordset);$x++){ $current_col = mysql_field_name($recordset,$x); $rows[$current_col][] = $row_recordset[$current_col]; } }while($row_recordset = mysql_fetch_assoc($recordset)); return $rows;
later you can use JSON to encode your array…
$JSON = new jsonService(); return $JSON->encode($rows);
Easy isn’t?

