How to add SQL elements to an array in PHP

Posted by DanLeaningphp on Stack Overflow See other posts from Stack Overflow or by DanLeaningphp
Published on 2011-01-13T02:24:00Z Indexed on 2011/01/13 2:53 UTC
Read the original article Hit count: 296

Filed under:
|
|

So this question is probably pretty basic.

I am wanting to create an array from selected elements from a SQL table.

I am currently using:

$rcount = mysql_num_rows($result);

for ($j = 0; $j <= $rcount; $j++)
{
    $row = mysql_fetch_row($result);
    $patients = array($row[0] => $row[2]);
}

I would like this to return an array like this:

$patients = (bob=>1, sam=>2, john=>3, etc...)

Unfortunately, in its current form, this code is either copying nothing to the array or only copying the last element.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql