douyou2368 2018-10-16 05:33
浏览 69
已采纳

使用php从mysql下载html数据中的列表

I have a drop-down list in HTML, where the contents need to load from my MySQL DB using PHP.

I tried like the following; but no option is loading.

<strong> Select Data </strong> 
<select name="data1"> 
     <option value=""> NONE </option> 
<?php

$dd_res=mysql_query("Select relation FROM relation_names");
while($r=mysql_fetch_row($dd_res))
{ 
     echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>

Also i tried like this:

$q="SELECT DISTINCT *relation FROM relation_names";
$rs=mysql_query($q);
if($rs && mysql_num_rows($rs))
{
    while($rd=mysql_fetch_object($rs))
    { 
        echo("<option value='$rd->id'>$rd->name</option>");
    }
}

But I don't know why these are not working, kindly help me.

i tried the code below as per comment. but option names are not diplaying.what i get when i try that code

  • 写回答

2条回答 默认 最新

  • doufei2194 2018-10-18 06:58
    关注
    <strong> Select Data </strong> 
    <select name="data1"> 
    <option value=""> NONE </option> 
    <?php
    // Mysql db connection
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    // Perform queries 
    $rs = mysqli_query($con,"SELECT DISTINCT relation FROM relation_names");
    if($rs && mysqli_num_rows($rs))
    {
         while($rd=mysqli_fetch_object($rs))
         { 
             echo("<option value='$rd->id'>$rd->relation</option>");
         }
    }
    mysqli_close($con);
    ?>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题