1

伙计们,我对此有点问题

我有一个如下所示的选择语句

$stmt = $conn->prepare("SELECT * FROM Product WHERE id=:id");
    $stmt->bindParam('id',$id);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    if($row) {];
        ?>  
<div>  <?echo $row['Name']; >? </div>
<div>  <?echo $row['Type']; >? </div>


$stmt1 = $conn->prepare("SELECT * FROM user WHERE Username IN ('john', 
                                'sarah', 'james')");
        $stmt1->bindParam('id',$id);
        $stmt1->execute();
        $row1 = $stmt1->fetch(PDO::FETCH_ASSOC);
        if($row1) {];
            ?>  
    <div>  <?echo $row1['username']; ?>' </div>

      <? } ?> //closing for the second select statement 

<? } ?>  //closing for the first select statement

问题

页面渲染但我使用SELECT * FROM user WHERE Username IN ('john', 'sarah', 'james')它的地方只显示一个名称而不是 3 个名称。

我知道 select 语句有效,因为我在 phpmyadmin 中尝试过,它确实显示了所有 3 个名称

4

2 回答 2

0

Try:

Instead of if($row1) use while($row1)

With if the loop only runs once but with while it runs as long there are results to loop through.

于 2013-08-25T12:30:20.450 回答
0

尝试这个:

while($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
?>  
<div><?php echo $row1['username']; ?> </div>
<?php } ?>

同时删除$stmt1->bindParam('id',$id);

因为if($row1)只读取第一行,您需要通过$stmt1->fetch(PDO::FETCH_ASSOC)每次调用来循环输出以检索行。

于 2013-08-25T12:37:00.497 回答