哪里和while循环似乎不起作用

时间:2018-08-15 06:22:38

标签: php mysql

我想在引导程序中使用“ where”和“ and”子句选择一些值。该代码仅选择一个值,而数据库中有多个。代码如下:

<ul class="collapsible collapsible-accordion">
                      <li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i>ENGLISH LANGUAGE<i class="fa fa-angle-down rotate-icon"></i></a>
                    <?php
                            $sql = "SELECT note_id, class, subject, topic, content, author FROM notes  WHERE subject = 'ENGLISH LANGUAGE' AND class = 'JHS 2'";
                            $result = $pdo->query($sql);
                            $result->setFetchMode(PDO::FETCH_ASSOC);
                            while($row=$result->fetch()){

                                ?>

                        <div class="collapsible-body">
                            <ul class="list-unstyled">
                                <?php echo '<li>'?><a data-id="<?php echo $key['note_id']?>" href="<?php echo $key['note_id']?>" class="waves-effect" id="getUser"><?php echo $key['topic']?></a>
                                <?php echo '</li>';?>
                                <li><a href="#" class="waves-effect">Registration form</a>
                                </li>
                            </ul>
                        </div>
                    </li>
                    <?php


                            }



                    ?>
                </ul>

1 个答案:

答案 0 :(得分:0)

如果要循环获取多个记录,则应使用方法::fetch_row()PHP.net)。

因此您的代码应如下所示:

                <ul class="collapsible collapsible-accordion">
                      <li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i>ENGLISH LANGUAGE<i class="fa fa-angle-down rotate-icon"></i></a>
                    <?php
                            $sql = "SELECT note_id, class, subject, topic, content, author FROM notes  WHERE subject = 'ENGLISH LANGUAGE' AND class = 'JHS 2'";
                            $result = $pdo->query($sql);
                            $result->setFetchMode(PDO::FETCH_ASSOC);
                            while($row=$result->fetch_row()){

                                ?>

                        <div class="collapsible-body">
                            <ul class="list-unstyled">
                                <?php echo '<li>'?><a data-id="<?php echo $key['note_id']?>" href="<?php echo $key['note_id']?>" class="waves-effect" id="getUser"><?php echo $key['topic']?></a>
                                <?php echo '</li>';?>
                                <li><a href="#" class="waves-effect">Registration form</a>
                                </li>
                            </ul>
                        </div>
                    </li>
                    <?php
                            }
                    ?>
                </ul>

通过使用::fetch()方法,您正在获取整个结果数组/对象。