从SQL Select with PHP输出一个关联数组

时间:2011-06-04 20:06:58

标签: php sql associative-array

我认为我的想法只是画了一个空白,但基本上,我想从各种sql结果创建一个关联数组

数组需要看起来像:

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

这是我的SQL语句

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

谢谢!

编辑: 此外,sql语句

可以选择多个行
$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 个答案:

答案 0 :(得分:3)

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

附录

Dunno你想要回应什么。无论如何,正确的语法是:

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

答案 1 :(得分:0)

这将为您提供mysql结果集中的关联数组:

$assoc = mysql_fetch_assoc ($res);