Leomel
Leomel

Reputation: 199

how to do 2 while loop mysqli_query in PHP?

My code below does not work properly:

<?php
$result1 = mysqli_query($conn, "SELECT * FROM bicycle_list");
while($res1 = mysqli_fetch_assoc($result1)){
    $bike_id = $res1['bike_id']; 
    $result2 = mysqli_query($conn, "SELECT * FROM bicycle WHERE bicyle_id = $bike_id");
    while($res2 = mysqli_fetch_assoc($result2)){
?> 
        <td><?php echo $res2['name']; ?></td>
<?php
    }
}
?>

how do i correct this?

Upvotes: 0

Views: 493

Answers (1)

Andy Lester
Andy Lester

Reputation: 93676

You want to use a SQL JOIN. Your basic query will look like this:

SELECT *
FROM bicycle_list INNER JOIN bicycle ON bicycle.bicycle_id = bicycle_list.bikeid;

But you should really read a tutorial about how SQL JOINs work rather than just using this code as-is.

Upvotes: 3

Related Questions