我如何从SQL数据库的一列填充php数组?

时间:2011-01-26 12:36:25

标签: php mysql arrays

假设我有一个名为“pcode”的mysql数据库只有一个名为“uk_postcodes”的表,我想用第一列填充我的数组,称为“postcodes”。以下代码缺少什么:

    //link to mysql server
    $link = mysql_connect('localhost', 'root', 'root');
    mysql_select_db('pcode'); 

    $sql = "SELECT * FROM `ukpostcodes`";
    $query = mysql_query($sql);

$var = array();

1 个答案:

答案 0 :(得分:7)

$sql = "SELECT postcodes FROM ukpostcodes";
$result = mysql_query($sql);

$var = array();
while ($row = mysql_fetch_array($result)) {
  $var[] = $row['postcodes'];
}