0

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'];
                }

2 Answers 2

3
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";
}
Sign up to request clarification or add additional context in comments.

4 Comments

you need to set an avatar, the world needs to know what lightning incarnate looks like L-/
@AJ: I will explain you why I am doing this (if you want). (in pvt of course)... lol anyway :)
Cool thanks... is there any way to make sure I'm doing it right to echo it out? This is what I tried (See edit above)
Because it wasn't working... but it was because of another issue I had... not that. Thanks!
0

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

$assoc = mysql_fetch_assoc ($res);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.