Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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
PHP循环查询不工作_Php_Mysql - Fatal编程技术网

PHP循环查询不工作

PHP循环查询不工作,php,mysql,Php,Mysql,我需要处理2个表,我尝试了内部连接,4个结果的位置,它只显示了一个表中的两个结果,我正在粘贴两个查询,请查看它并帮助我,我将感谢你们所有人 <? $query5 = "SELECT * FROM escorts_touring order by es_tou_id"; $result5 = mysql_query($query5); while($row5 = @mysql_fetch_array ($result5, MYSQL_ASSOC)) {

我需要处理2个表,我尝试了内部连接,4个结果的位置,它只显示了一个表中的两个结果,我正在粘贴两个查询,请查看它并帮助我,我将感谢你们所有人

<? 
    $query5 = "SELECT * FROM escorts_touring order by es_tou_id";
    $result5 = mysql_query($query5);
    while($row5 = @mysql_fetch_array ($result5, MYSQL_ASSOC)) 
    {
        $es_touring_city = $row5['es_touring_city'];
    }

?>

<?php 

        echo $sql="SELECT e.es_id, e.es_sex, e.service_type, 
                          e.working_name,  t.es_tou_id, t.es_id, 
                          t.es_touring_city, t.es_touring_start_date, 
                          t.es_touring_end_date 
                    FROM escorts AS e 
                        INNER JOIN escorts_touring AS t 
                            ON e.es_id = t.es_id 
                    where es_touring_city = '$es_touring_city'";
        $result=mysql_query($sql); 
        $rowcount=mysql_num_rows($result);
        $counter=0;
        $count=0;

        while($row = @mysql_fetch_array ($result, MYSQL_ASSOC)) 
        {

            if($counter++%4==0)print"</div><div class=\"row\"></div>";

            $es_sex  =$row['es_sex'];
            $service_type=$row['service_type'];
            $working_name=$row['working_name'];
            $es_id=$row['es_id'];
            $es_tou_id = $row['es_tou_id'];
            $es_touring_city = $row['es_touring_city'];
            $es_touring_start_date = $row['es_touring_start_date'];
            $es_touring_end_date =$row['es_touring_end_date'];

            $newstartDate = date("dS F, Y", strtotime($es_touring_start_date));
            $newendDate = date("dS F, Y", strtotime($es_touring_end_date));

            $query = "SELECT * FROM escorts_image where es_id = $es_id";
            $result_image = @mysql_query ($query);
            $row_image = @mysql_fetch_array ($result_image, MYSQL_ASSOC);
            $image = $row_image['image'];
            $dest="uploads";
        ?>
它显示了4个结果

但当我在第二次查询中使用$es_touring_city时,它只显示了两个带有图像的结果

如果我不是很清楚的话


谢谢,

将所有内容合并到一个查询中。对结果进行排序,以便相同
es_id
的所有行都在一起。然后检查
$row['es_id']
何时更改,并开始一个新行

<?php

$sql="SELECT e.es_id, e.es_sex, e.service_type, 
             e.working_name,  t.es_tou_id, t.es_id, 
             t.es_touring_city, t.es_touring_start_date, 
             t.es_touring_end_date, i.image
      FROM escorts AS e 
      INNER JOIN escorts_touring AS t ON e.es_id = t.es_id
      INNER JOIN (SELECT es_id, MAX(image) AS image
                  FROM escorts_image
                  GROUP BY es_id) AS i ON e.id = i.es_id
      ORDER BY e.es_id";
$result = mysql_query($sql);
$last_esid = null;
$counter = 0;

$dest = "uploads";

while ($row = mysql_fetch_assoc($result)) {
    $es_id=$row['es_id'];
    if ($counter++ == 4 || $es_id != $last_esid) {
        if ($last_esid) {
            echo "</div>";
        }
        echo "<div class='row'></div>";
        $last_esid = $es_id;
        $counter = 0;
    }

    $es_sex  =$row['es_sex'];
    $service_type=$row['service_type'];
    $working_name=$row['working_name'];
    $es_tou_id = $row['es_tou_id'];
    $es_touring_city = $row['es_touring_city'];
    $es_touring_start_date = $row['es_touring_start_date'];
    $es_touring_end_date =$row['es_touring_end_date'];
    $image = $row['image'];

    $newstartDate = date("dS F, Y", strtotime($es_touring_start_date));
    $newendDate = date("dS F, Y", strtotime($es_touring_end_date));

    // print the row
}

每次在新代码中使用数据库扩展时,它都会被弃用,并且已经存在多年,在PHP7中永远消失。如果你只是在学习PHP,那么就把精力花在学习
PDO
mysqli
数据库扩展上。您不应该使用这样的嵌套查询。他们效率很低。您应该重写为单个
join
ed查询。第二个查询中已经有连接,因此请将它们展开以涵盖父/子查询。不要使用
@
错误消音器。如果有错误,请先显示,然后再修复Them@MarkB,你能帮我吗?因为我对它了解不多,如果你按照你的建议稍微扩展一下代码,我会感谢你,我仍然在努力得到正确的结果。谢谢加载标量变量时,第一个查询及其关联的while循环将只记住结果集的最后一行
$es_touring_city=$row5['es_touring_city']1到多次。只保留最后一行information@barmer,非常感谢,它似乎工作得很好,我已经编辑了它,它的工作正如我所希望的,只有一个问题,因为每个成员上传了大约20张照片,具有相同的es_id,并且第一张图像总是显示在屏幕上,而不是任何图像,因此,如果只是这部分指导我,那么一切都将完成@barmer,再次非常感谢我也找到了图像的解决方案,非常感谢!原始查询只是随机选择了一个图像。我需要选择一个图像,所以我任意选择了
MAX(image)
<?php

$sql="SELECT e.es_id, e.es_sex, e.service_type, 
             e.working_name,  t.es_tou_id, t.es_id, 
             t.es_touring_city, t.es_touring_start_date, 
             t.es_touring_end_date, i.image
      FROM escorts AS e 
      INNER JOIN escorts_touring AS t ON e.es_id = t.es_id
      INNER JOIN (SELECT es_id, MAX(image) AS image
                  FROM escorts_image
                  GROUP BY es_id) AS i ON e.id = i.es_id
      ORDER BY e.es_id";
$result = mysql_query($sql);
$last_esid = null;
$counter = 0;

$dest = "uploads";

while ($row = mysql_fetch_assoc($result)) {
    $es_id=$row['es_id'];
    if ($counter++ == 4 || $es_id != $last_esid) {
        if ($last_esid) {
            echo "</div>";
        }
        echo "<div class='row'></div>";
        $last_esid = $es_id;
        $counter = 0;
    }

    $es_sex  =$row['es_sex'];
    $service_type=$row['service_type'];
    $working_name=$row['working_name'];
    $es_tou_id = $row['es_tou_id'];
    $es_touring_city = $row['es_touring_city'];
    $es_touring_start_date = $row['es_touring_start_date'];
    $es_touring_end_date =$row['es_touring_end_date'];
    $image = $row['image'];

    $newstartDate = date("dS F, Y", strtotime($es_touring_start_date));
    $newendDate = date("dS F, Y", strtotime($es_touring_end_date));

    // print the row
}