Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
foreach循环中的PHP while循环只检索一条记录_Php - Fatal编程技术网

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

foreach循环中的PHP while循环只检索一条记录,php,Php,好吧,我很困惑。出于某种原因,这种循环组合只返回一个城市记录。我有一组邮政编码($locationzips)。我可以遍历它们,但只返回一个关联的城市,即使foreach循环正确完成。我已经检查了数据库表,那里的邮政编码是正确的城市。我没看见什么? foreach ($locationzips as $key => $zip) { $getcities = mysql_query("SELECT * FROM us WHERE code = '$zip'"); echo 't

好吧,我很困惑。出于某种原因,这种循环组合只返回一个城市记录。我有一组邮政编码($
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>';
    } 
}
foreach($locationzips作为$key=>$zip){
$getcities=mysql_查询(“从我们这里选择*,其中代码=“$zip”);
回显“测试邮政编码:”.$zip.“
”; 而($row=mysql\u fetch\u assoc($getcities)){ $city=$row['city']; 回显“测试城市:”.$city.“
”; } }
这将返回:

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>
测试邮政编码:34110
测试城市:那不勒斯
测试邮政编码:34145
测试邮政编码:34135
测试邮政编码:33928
测试邮政编码:33901
测试邮政编码:33904

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

    $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>';
    }
$cities=mysql\u查询(“从美国选择*);
$cityArray=array();
而($row=mysql\u fetch\u row($cities,mysql\u ASSOC)){
$cityArray[$row['code']]=$row['city'];
}
foreach($locationzips作为$zip){
打印“测试邮政编码:”.$Zip.“
”; 打印“测试城市:”。$cityArray[$zip]。
; }
我可能会对不使用mysql扩展(即mysql_*)函数大发雷霆……我不会,但只会说,你应该考虑替换它们;PHP将弃用并删除它们。考虑使用PDO或MySQL。我想你会发现上面的代码块更有效,更容易测试/调试。

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());
        }
    }
}

c) mysql扩展被标记为depreacted。改用类似的方法

count(mysql\u fetch\u assoc($getcities))告诉您什么?sql返回什么?不是在php中,而是在phpmyadmin中?您的查询可能有错误?请尝试使用
$city[]
而不是
$city
(如果有多个结果),然后编写
var\u dump($city)
使用
mysql\u error()
查看查询是否实际工作。我非常确定
$getcities
查询没有像您预期的那样返回结果。使用
mysql\u num\u行($getcities)
echo“SELECT*FORM-us,其中code='$zip'”<?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