I have been trying to populate a dropdown with a list of names from an mysql table. 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.
<?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:
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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.