虽然嵌套lops在php中从sql数据库中获取数据

时间:2022-10-16 12:10:32
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>".while($row = $result1->fetch_assoc())
{echo'<img src='".$row["image"]."'>;}
      .</aside>}";

I have been using while loops for fetching data from table. My connection is working is perfect but I need another loop in the first that is not working. Isn't it the good way?

我一直在使用while循环从表中获取数据。我的连接工作是完美的,但我需要在第一个无效的另一个循环。这不是好方法吗?

Update: I tried to finish echo and again started as follow but still an error

更新:我试图完成回声并再次启动如下,但仍然是一个错误

while($row = $result->fetch_assoc()) {
     echo "<div id='post'><h1>"
     .$row["heading"].
     "</h1><div class='post-side'><img class='post-image' src='"
     .$row["image"].
     "'><div class='post-data'><p><strong>Age: </strong><span>$age</span></p><p><strong>Date of birth: </strong><span>"
     .$row["day"].
     "-"
     .$row["month"].
     "-"
     .$row["year"].
     "</span></p></div></div></div><div class='description'><p>"
     .$row["description"].
     "</p></div><div class='bottom-related'><aside class='related-post'>";
     while($row = $result1->fetch_assoc())
     {echo"<img src='"
     .$row["image"].
     "'>/";}.echo"</aside><aside class='ad2'>".$includead."</aside></div>";

   }
   echo "</div>";
 } else {
 echo "No table found";
 }

 $conn->close();

2 个解决方案

#1


1  

You're trying to concatenate to a string a WHILE loop; this is wrong. You should echo your first part, end with it and then do your while loop, and echo the end afterwards:

你试图连接一个WHILE循环的字符串;这是错的。你应该回应你的第一部分,结束它然后做你的while循环,然后回声结束:

Your quotes are a bit messed up as well

你的报价也有些搞砸了

if ($result->num_rows > 0) 
{
    echo "<div id='post'><h1>".$row["heading"]."</h1>
          <aside class='related-post'>";

    while($row = $result1->fetch_assoc())
    {
      echo'<img src="'.$row["image"].'">';
    }
    echo '</aside>';
 }

#2


0  

You can't concatene while with String, it's a syntaxic error
Also, you have a probleme when trying to echo a String, you can use this syntax:

使用String时无法连接,这是一个语法错误另外,在尝试回显String时遇到问题,可以使用以下语法:

echo "PHP"; // will evaluate PHP variables and whitespace inside a string
echo 'PHP'; // will evaluate nothing;

But you can not start flushing a string ' and finish by " or vice-versa.
Here the correct code :

但你不能开始刷字符串'并完成“或反之亦然。这里正确的代码:

<?php
$sql        = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1       = "SELECT * FROM today WHERE day='$day'";
$result     = $conn->query($sql);
$result1    = $conn->query($sql1);

    if ($result->num_rows > 0) {
        echo "<div id='post'><h1>" . $row["heading"] . "</h1><aside class='related-post'>";

        while($row = $result1->fetch_assoc()) {
            echo'<img src="' . $row["image"] .'">';
        }
        echo "</aside>";
    }

#1


1  

You're trying to concatenate to a string a WHILE loop; this is wrong. You should echo your first part, end with it and then do your while loop, and echo the end afterwards:

你试图连接一个WHILE循环的字符串;这是错的。你应该回应你的第一部分,结束它然后做你的while循环,然后回声结束:

Your quotes are a bit messed up as well

你的报价也有些搞砸了

if ($result->num_rows > 0) 
{
    echo "<div id='post'><h1>".$row["heading"]."</h1>
          <aside class='related-post'>";

    while($row = $result1->fetch_assoc())
    {
      echo'<img src="'.$row["image"].'">';
    }
    echo '</aside>';
 }

#2


0  

You can't concatene while with String, it's a syntaxic error
Also, you have a probleme when trying to echo a String, you can use this syntax:

使用String时无法连接,这是一个语法错误另外,在尝试回显String时遇到问题,可以使用以下语法:

echo "PHP"; // will evaluate PHP variables and whitespace inside a string
echo 'PHP'; // will evaluate nothing;

But you can not start flushing a string ' and finish by " or vice-versa.
Here the correct code :

但你不能开始刷字符串'并完成“或反之亦然。这里正确的代码:

<?php
$sql        = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1       = "SELECT * FROM today WHERE day='$day'";
$result     = $conn->query($sql);
$result1    = $conn->query($sql1);

    if ($result->num_rows > 0) {
        echo "<div id='post'><h1>" . $row["heading"] . "</h1><aside class='related-post'>";

        while($row = $result1->fetch_assoc()) {
            echo'<img src="' . $row["image"] .'">';
        }
        echo "</aside>";
    }