从数据库中下拉列表值 (PHP/SQL)

我只是想知道下面的代码是否最有效地使用 php/sql 中的数据库信息填充下拉列表。


                //GET ALL SONGS FROM DATABASE

                $query = $dbConn->query("SELECT track_id, track_title FROM track");


                echo '<select class="feild" name="songDrop">';//MAKE THE DROPDOWN 


                echo '<option value="none">Choose the Song to add</option>';


                // Loop through the query results, displaying the options one by one

                while ($row = $query->fetch_assoc()) {

                     echo '<option value="'.$row['track_id'].'">'.$row['track_title'].'</option>';

                }


                echo '</select>';


翻翻过去那场雪
浏览 82回答 1
1回答

慕田峪7331174

尝试这个 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //GET ALL SONGS FROM DATABASE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = $dbConn->query("SELECT track_id, track_title FROM track");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<select class="feild" name="songDrop">';//MAKE THE DROPDOWN&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<option value="none">Choose the Song to add</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through the query results, displaying the options one by one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ($row = mysqli_fetch_array($query)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '<option value="'.$row['track_id'].'">'.$row['track_title'].'</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</select>';
打开App,查看更多内容
随时随地看视频慕课网APP