简体   繁体   中英

output an associative array, from SQL Select with PHP

I think my mind is just drawing a blank, but basically, I want to create an associative array from various sql results

The array needs to look like:

$people = array(
     "+1123456789" => "Phil"
);

Here is my SQL Statement

$sql = " SELECT phonenumber6, firstName FROM members WHERE departmentID = 4 AND phonenumber6 <> '+1';";

Thanks!

Edit: Also, there can be multiple rows that were selected by the sql statement

$sql = " SELECT phonenumber6, firstName FROM members WHERE departmentID = 4 AND phonenumber6 <> '+1';";

                $result = mysql_query($sql);

                while($row=mysql_fetch_assoc($result)) {
                  echo $people[$row['phonenumber6']] = $row['firstName'];
                }
while($row=mysql_fetch_assoc($query)) {
  $people[$row['phonenumber6']] = $row['firstName'];
}

Addendum

Dunno what you want to echo. Anyway the right syntax is:

while($row=mysql_fetch_assoc($query)) {
  $people[$row['phonenumber6']] = $row['firstName'];
  echo $row['phonenumber6']. '=> '.$row['firstName']."<br />\n";
}

This will give you an associated array from a mysql result set:

$assoc = mysql_fetch_assoc ($res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM