在foreach循环中的PHP while循环只检索一条记录

时间:2012-10-15 11:33:06

标签: php

好的,我很神秘。由于某种原因,这种循环组合只返回一个城市记录。我有一系列邮政编码($ locationzips)。我可以遍历它们,但只返回一个关联的城市,即使foreach循环正确完成。我检查了数据库表,邮政编码与正确的城市一致。我没看到什么?

foreach ($locationzips as $key => $zip) {
    $getcities = mysql_query("SELECT * FROM us WHERE code = '$zip'");
    echo 'test zip: '.$zip.'<br>';
    while($row = mysql_fetch_assoc($getcities)) {
        $city = $row['city'];
        echo 'test city: '.$city.'<br>';
    } 
}

返回:

test zip: 34110<br>
test city: Naples<br>
test zip: 34145<br>
test zip: 34135<br>
test zip: 33928<br>
test zip: 33901<br>
test zip: 33904<br>

2 个答案:

答案 0 :(得分:1)

在不知道你的桌子是什么样的情况下,我会说这是一种昂贵的方式。我建议从你的表中创建一个数组的所有城市,并用邮政编码键入它们。您将无需多次运行查询。

    $cities = mysql_query("SELECT * from us");
    $cityArray = array();
    while($row = mysql_fetch_row($cities, MYSQL_ASSOC)){
        $cityArray[$row['code']] = $row['city'];
    }

    foreach($locationzips as $zip) {
        print 'Test Zip:' . $zip . '<br>';
        print 'Test City:' . $cityArray[$zip] .'<br>';
    }

我可以开始咆哮不使用,mysql扩展(即mysql_ *)函数......我不会但只会说,你应该考虑更换它们; PHP将弃用并删除它们。考虑使用PDO或MySQLi。我想你会发现上面的块更有效,更容易测试/调试。

答案 1 :(得分:0)

a)我无法使用以下脚本+示例数据

重现该行为
<?php
mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());
setup();
$locationzips = array(34110,34145,34135,33928,33901,33904);

foreach ($locationzips as $key => $zip) {
    $getcities = mysql_query("SELECT * FROM tmp_us WHERE code = '$zip'") or die(mysql_error());
    echo 'test zip: '.$zip."\n";
    while($row = mysql_fetch_assoc($getcities)) {
        $city = $row['city'];
        echo 'test city: '.$city."\n";
    }
}

function setup() {
    mysql_query('
        CREATE TEMPORARY TABLE tmp_us (
            id int auto_increment,
            city varchar(64),
            code varchar(16),
            primary key(id)
        )
    ') or die(mysql_error());

    for($code=33901; $code<=34145; $code++) {
        foreach(range('a','d') as $foo) {
            $city = $code.'_'.$foo;
            $stmt = sprintf("INSERT INTO tmp_us (city,code) VALUES('%s','%s')", $city, $code);
            mysql_query($stmt) or die(mysql_error());
        }
    }
}

b)例如,您不需要多次查询使用IN运算符

<?php
mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());
setup();
$locationzips = array(34110,34145,34135,33928,33901,33904);


$stmt = sprintf("SELECT * FROM tmp_us WHERE code IN ('%s') ORDER BY code", join("','", $locationzips));
$getcities = mysql_query($stmt) or die(mysql_error());
$current_code = null;
while( false!==($row=mysql_fetch_assoc($getcities)) ) {
    if ( $current_code!=$row['code'] ) {
        $current_code = $row['code'];
        echo 'zip: ', $current_code, "\n";
    }
    echo '  city: ', $row['city'], "\n";
}

function setup() {
    mysql_query('
        CREATE TEMPORARY TABLE tmp_us (
            id int auto_increment,
            city varchar(64),
            code varchar(16),
            primary key(id)
        )
    ') or die(mysql_error());

    for($code=33901; $code<=34145; $code++) {
        foreach(range('a','d') as $foo) {
            $city = $code.'_'.$foo;
            $stmt = sprintf("INSERT INTO tmp_us (city,code) VALUES('%s','%s')", $city, $code);
            mysql_query($stmt) or die(mysql_error());
        }
    }
}

打印

zip: 33901
  city: 33901_a
  city: 33901_b
  city: 33901_c
  city: 33901_d
zip: 33904
  city: 33904_d
  city: 33904_c
  city: 33904_b
  city: 33904_a
zip: 33928
  city: 33928_a
  city: 33928_b
  city: 33928_c
  city: 33928_d
zip: 34110
  city: 34110_d
  city: 34110_c
  city: 34110_b
  city: 34110_a
zip: 34135
  city: 34135_d
  city: 34135_c
  city: 34135_b
  city: 34135_a
zip: 34145
  city: 34145_a
  city: 34145_b
  city: 34145_c
  city: 34145_d

c)mysql扩展名被标记为depreacted。使用类似PDO的内容