douyin2883 2016-10-16 06:17 采纳率: 0%
浏览 22

如何从整个列中选择数据并将其作为选择标记中的单独选项显示。

I think this question is going to be rather interesting. So, as the title suggests, I am trying to make a dynamic select field. Basically, when the time comes my web app will connect to my database, and pull the entire row that matches my query. My question is this: How can I present the data from ONE of the columns within the row as INDIVIDUAL options within the select tag? This is what I am trying right now:

<select>
    <?php
        $servername = "127.0.0.1";
        $username = "root";
        $password = "";
        $dbname = "people";
        $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        }else{
            $search_query = "SELECT * FROM people_list WHERE person_id = $_SESSION[person_id]";
            $result = $conn->query($search_query);
            while($person_result_list = mysqli_fetch_assoc($result)){
            echo "

            <option value='$result[person_result_list]'>'$result[person_name]', '$result[person_result_list]'</option>

            ";



            }

        }

    ?>
    </select>

This is returning an error: Cannot use object of type mysqli_result as array Which should be pretty straight forward to fix, but I just can't figure out the workaround to this. Any ideas?

Thanks all!

  • 写回答

2条回答 默认 最新

  • dp198879 2016-10-16 06:24
    关注

    you can use GROUP_CONCAT in the query. It will return you a comma separated string of the values of your target column only. You then can split this string to make it an array to use for the select dropdown.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。