douang1243 2014-02-13 19:01
浏览 45
已采纳

在html select中显示MySQL查询

I am still a beginner with php and MySQL. I am having trouble getting rows from my database to display in an html select drop down box. I have researched it and it seems like my code should be good. The campaigns table as a row titled name. This is the row I am wanting to echo into the drop down. The drop down shows, however there is no content in it. Not sure what I am missing here...

Here is the code

<?php
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = mysqli_query($con,"SELECT * FROM campaigns"); 

echo '<select name="campaignChange">';

while ($row = mysql_fetch_array($query)) {
    echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}

echo '</select>';

?>
  • 写回答

1条回答 默认 最新

  • doupai6875 2014-02-13 19:02
    关注

    You are mixing mysql and mysqli syntax.

    You should change:

    $query = mysql_query($con,"SELECT * FROM campaigns");
    

    to:

    $query = mysqli_query($con,"SELECT * FROM campaigns");
    

    and:

    while ($row = mysql_fetch_array($query)) {
    

    to:

    while ($row = mysqli_fetch_array($query)) {
    

    By the way, you should add error handling. If you add this to the top:

    mysqli_report(MYSQLI_REPORT_ALL);
    

    mysqli will throw exceptions so you will always know what goes wrong exactly. As long as you use mysqli functions of course...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器