猿问

在 mysqli WHERE LIKE 语句中使用 $var

我正在尝试获取某个地区的项目列表。


我想要区域名称作为标题然后该区域中的所有项目都在下表中,即


标题 - Region1


表 - Region1 项目


标题 - Region2


表 - Region2 项目


标题 - Region3


表 - Region3 项目


标题 - Region4


表 - Region4 项目


我可以输出:


标题


标题


标题


如果我使用 LIKE 'Actual Region Name',我可以在表中输出区域详细信息


我想使用 LIKE $region 输出,所以我不需要为每个“实际区域名称”编写新语句。


2 SELECT 查询:


$region = mysqli_query($conn,"SELECT DISTINCT region FROM country") or die($conn->error);

$result = mysqli_query($conn,"SELECT * FROM country WHERE region LIKE '$region'") or die($conn->error);

输出 1


这将每个区域输出为


地区名称


$i = 0;

while($row = $region->fetch_assoc())

{

 if ($i == 0) {

 foreach ($row as $value) {

 echo "<p>" . $value . "</p>";

                      }


             }

}

输出 2


这将输出所有区域数据并将其放入表格中。


echo "This is table Build";

echo "<table border='1'>";


$i = 0;

while($row = $result->fetch_assoc())

{

    if ($i == 0) {

      $i++;

      echo "<tr>";

      foreach ($row as $key => $value) {

        echo "<th>" . $key . "</th>";

      }

      echo "</tr>";

    }

    echo "<tr>";

    foreach ($row as $value) {

      echo "<td>" . $value . "</td>";

    }

    echo "</tr>";

}

echo "</table>";


mysqli_close($conn);


?>

OUTPUT 1 和 OUTPUT 2 分别工作。我不能让他们一起工作


www说
浏览 125回答 1
1回答

慕森王

您可以region从查询中获取所有名称,然后将其传递给您next query并打印record与该查询相关的所有名称,即:<?php$region = mysqli_query($conn,"SELECT DISTINCT region FROM country") or die($conn->error);if($region->num_rows > 0)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;while($row = $region->fetch_assoc())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//printing region name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<p>" . $row['region'] . "</p>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//only those row will be retrieve which belong to current region name&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$result = mysqli_query($conn,"SELECT * FROM country WHERE region LIKE '$row['region']'") or die($conn->error);//printing table&nbsp;&nbsp; &nbsp; &nbsp; echo "This is table Build";&nbsp; &nbsp; &nbsp; echo "<table border='1'>";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; while($row1 = $result->fetch_assoc())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($row1 as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>" . $key . "</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; foreach ($row1 as $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $value . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; }&nbsp; &nbsp; echo "</table>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}}&nbsp; mysqli_close($conn);&nbsp; &nbsp; &nbsp; &nbsp; ?>
随时随地看视频慕课网APP
我要回答