PHP Filling an array from a MySql request

I am looking to create a data array to transfer another function that is populated from a database query, and I'm not sure how to do this.

$dataArray[0][1]; $qry = mysql_query("SELECT Id, name FROM users"); while($res = mysql_fetch_array($qry)) { $dataArray[$res['Id']][$res['name']] } 

Thanks in advance.

+4
source share
2 answers

It will look better

 $dataArray = array(); $qry = mysql_query("SELECT Id, name FROM users"); while($res = mysql_fetch_array($qry)) { $dataArray[$res['Id']] = $res['name']; } 

You can take a look at the PHP manual on how to declare and manipulate arrays.

+5
source

Below is the sniper code is very convenient ...

$ select = "WRITE YOUR SELECTION REQUEST"; $ queryResult = mysql_query ($ select);

 //DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS $data_array=array(); //STORE ALL THE RECORD SETS IN THAT ARRAY while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) { array_push($data_array,$row); } mysql_free_result($queryResult); //TEST TO SEE THE RESULT OF THE ARRAY echo '<pre>'; print_r($data_array); echo '</pre>'; 

thanks

+2
source

All Articles