5

I am looking to create an array of data to be pass to another function that is populated from a database query and am 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.

2
  • 1
    I spent about thirty seconds trying to understand how this code shows PHP polluting an array. Commented Nov 20, 2009 at 8:09
  • @bipedalShark: yeah that was kind of funny! Commented Nov 20, 2009 at 8:10

2 Answers 2

6

This would 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 how to declare and manipulate arrays.

Sign up to request clarification or add additional context in comments.

Comments

3

The below code sniper is very handy...

$select=" WRITE YOUR SELECT QUERY "; $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

1 Comment

This is excellent. The minimalist and most efficient answer in my honest opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.