[英]Trying to populate php dropdown with data from sql table
I have been trying to populate a dropdown with a list of names from an mysql table. 我一直在尝试用mysql表中的名称列表填充下拉列表。 The table has columns for Name, Date, Host, and Info. 该表具有“名称”,“日期”,“主机”和“信息”列。 I have looked around online, but I can't seem to find a solution to my issue. 我已经在网上四处张望,但似乎找不到解决我问题的方法。 I am pretty sure it connects to the database, but I keep getting the error Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\phpmsql\\delegates.php on line 16. 我非常确定它已连接到数据库,但是我不断收到错误通知:第16行的C:\\ xampp \\ htdocs \\ phpmsql \\ delegates.php中未定义的偏移量:1。
<?php
$db = new mysqli('127.0.0.1', 'root', '', 'munapp');
if($db->connect_errno) {
die('Sorry we having some connection problems');
}
$query = "SELECT Name FROM `conferences`";
$result2 = mysqli_query($db, $query);
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title> </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<select>
<?php echo $options;?>
</select>
</body>
</html>
You wore trying to access the second column from the result, but you only selected one column. 您穿着试图从结果中访问第二列,但是您只选择了一个列。
Instead of the query: 代替查询:
$query = "SELECT Name FROM `conferences`";
select all columns like this: 选择所有这样的列:
$query = "SELECT name, date, host, info FROM `conferences`";
更改$options
分配以从数组中的0
索引填充:
$options = $options."<option>$row2[0]</option>";
Like someone else pointed out, you need to access the first array element with an index of 0 instead of 1. Alternatively you could fetch your row like this: 就像其他人指出的那样,您需要使用索引0而不是1访问第一个数组元素。或者,您可以像这样获取行:
while(row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
//accesss value here
}
which will give you the result as an associative array and then inside the loop access the values by their column namens like this: 这将为您提供一个关联数组的结果,然后在循环内部按其列名访问值,如下所示:
$row2['Name']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.