如何在PHP中循环mysqli_query 2?

时间:2013-12-09 16:56:35

标签: php mysqli

我的代码无法正常运行:

<?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
    }
}
?>

我该怎么纠正这个?

1 个答案:

答案 0 :(得分:3)

您想使用SQL JOIN。您的基本查询将如下所示:

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

但是您应该阅读有关SQL JOIN如何工作的教程,而不仅仅是按原样使用此代码。