0

I have a loop inside other loop which is not working, this is the code:

    while($row = mysqli_fetch_array($result))
   {
    echo "<tr>";
    echo "<td>" . $row['rowId'] . "</td>";
    echo "<td>" . $row['startDate'] . "</td>";
    echo "<td>" . $row['eventName'] . "</td>";
    echo "<td>" . $row['betName'] . "</td>";
    $string1 = "SELECT * FROM newCell WHERE rowId ='";
    $string2 = $row['rowId']."'";
    $result2 = $string1.$string2;
    echo "<td>" . $result2 . "</td>";

    while($row2 = mysqli_fetch_array($result2))
    {
        echo "<td>" . $row2['odds'] . "</td>";

        echo "<td>" . $row2['outcomeName'] . "</td>";
    }
   echo "</tr>";
   }

When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?

superTramp
  • 165
  • 2
  • 3
  • 11

6 Answers6

0

Use:

$query = "SELECT ....";
$result2 = mysqli_query($db, $query);

while($row2 = mysqli_fetch_array($result2))
{
        echo "<td>" . $row2['odds'] . "</td>";

        echo "<td>" . $row2['outcomeName'] . "</td>";
}
q0re
  • 1,401
  • 19
  • 32
0

I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.

Probably you should have something like this

$result2 = mysqli_query($result2);
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Amit Gupta
  • 533
  • 6
  • 17
0

Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()

Community
  • 1
  • 1
RDK
  • 4,540
  • 2
  • 20
  • 29
0

Try This

<?php
while($row = mysqli_fetch_array($result))
   {
    echo "<tr>";
    echo "<td>" . $row['rowId'] . "</td>";
    echo "<td>" . $row['startDate'] . "</td>";
    echo "<td>" . $row['eventName'] . "</td>";
    echo "<td>" . $row['betName'] . "</td>";
    $string1 = "SELECT * FROM newCell WHERE rowId ='";
    $string2 = $row['rowId']."'";
    $result2 = $string1.$string2;
    echo "<td>" . $result2 . "</td>";
    $results = mysqli_query($db,$result2);
    while($row2 = mysqli_fetch_array($results))
    {
        echo "<td>" . $row2['odds'] . "</td>";

        echo "<td>" . $row['outcomeName'] . "</td>";
    }
   echo "</tr>";
   }
  ?>
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
0
  while($row = mysqli_fetch_array($result))
   {
    echo "<tr>";
    echo "<td>" . $row['rowId'] . "</td>";
    echo "<td>" . $row['startDate'] . "</td>";
    echo "<td>" . $row['eventName'] . "</td>";
    echo "<td>" . $row['betName'] . "</td>";
    $string1 = "SELECT * FROM newCell WHERE rowId ='";
    $string2 = $row['rowId']."'";
    $result2 = $string1.$string2;
    echo "<td>" . $result2 . "</td>";

    $result2 = mysqli_query($connection, $result2);

    while($row2 = mysqli_fetch_array($result2))
    {
        echo "<td>" . $row2['odds'] . "</td>";

        echo "<td>" . $row2['outcomeName'] . "</td>";
    }
   echo "</tr>";
   }
Mubin
  • 4,325
  • 5
  • 33
  • 55
0

Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .

Praveen D
  • 2,337
  • 2
  • 31
  • 43