我一直在尝试使用mysql表中的名称列表填充下拉列表。该表包含Name,Date,Host和Info的列。我在网上看过,但我似乎无法找到解决问题的方法。我很确定它连接到数据库,但我一直收到错误提示:未定义的偏移量:在第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>
答案 0 :(得分:0)
您试图从结果中访问第二列,但您只选择了一列。
而不是查询:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-role="page" id="page1">
<div data-role="header">
<h1>My page</h1>
</div>
<div role="main" class="ui-content">
<div id="mysliders">
<label for="slider-1">Slider:</label>
<input class="add" type="range" name="slider-1" id="slider-1" min="0" max="100" value="50" />
<label for="slider-2">Slider:</label>
<input class="add" type="range" name="slider-2" id="slider-2" min="0" max="100" value="40" />
<label for="slider-3">Slider:</label>
<input class="add" type="range" name="slider-3" id="slider-3" min="0" max="100" value="30" />
<label for="slider-4">Slider:</label>
<input class="add" type="range" name="slider-4" id="slider-4" min="0" max="100" value="65" />
<label for="slider-5">Slider:</label>
<input class="add" type="range" name="slider-5" id="slider-5" min="0" max="100" value="11" />
<label for="slider-6">Slider:</label>
<input class="add" type="range" name="slider-6" id="slider-6" min="0" max="100" value="90" />
</div>
<hr />
<p>Total: <strong id="total"></strong></p>
</div>
<div data-role="footer" data-position="fixed">
<h1>Footer</h1>
</div>
</div>
选择所有列,如下所示:
$query = "SELECT Name FROM `conferences`";
答案 1 :(得分:0)
将$options
分配更改为从数组中的0
索引填充:
$options = $options."<option>$row2[0]</option>";
答案 2 :(得分:0)
像其他人指出的那样,你需要访问索引为0而不是1的第一个数组元素。或者你可以像这样获取你的行:
while(row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
//accesss value here
}
它会将结果作为关联数组提供给你,然后在循环内部通过它们的列名来访问这些值:
$row2['Name']