Although nested lops in php extract data from sql database

$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 used a while loop to retrieve data from a table. My connection works fine, but I need another loop in the first that doesn't work. Isn't that a good way?

Update: I tried to finish the echo and started again as it should, 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();
+4
source share
2 answers

You are trying to bind a string to a WHILE loop; this is not true. You have to repeat your first part, end it, and then execute a while loop and then discard the end:

Your quotes are also a bit confused.

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
source

concatene while ,
, String, :

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

' " .
:

<?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>";
    }
0

Source: https://habr.com/ru/post/1612382/


All Articles