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
  • 115
  • 1
  • 2
  • 10

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,365
  • 17
  • 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
  • 80,876
  • 9
  • 135
  • 306
Amit Gupta
  • 493
  • 6
  • 16
0

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

Community
  • 1
  • 1
RDK
  • 4,475
  • 2
  • 18
  • 27
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,480
  • 5
  • 30
  • 73
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
  • 3,597
  • 4
  • 28
  • 49
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,258
  • 2
  • 23
  • 40