[英]mysqli query returns true but fetch_assoc doesn't work
I am using PHP and MySQL (mysqli) in XAMPP, I have a drop down, and the user must choose one, then a query is used to find the id of the value that has been chosen but it does not work. 我在XAMPP中使用PHP和MySQL(mysqli),我有一个下拉列表,用户必须选择一个,然后使用查询来查找已选择的值的id,但它不起作用。 I have already done this three more times and it worked but this one doesn't.
我已经做了三次这样做了,但是这个没有用。
$sql = "SELECT foo_id FROM foo_table
WHERE foo_name = 'bar';";
$res = $conn->query($sql);
for ($i = 0; $i < 500; $i++) {
$row = $res->fetch_assoc();
echo $row[row["foo_id"]]
}
The problem is that fetch_assoc does not return anything even though the $res variable returns true. 问题是即使$ res变量返回true,fetch_assoc也不会返回任何内容。
Edit: I forgot to mention that running the query in phpmyadmin returns results normally. 编辑:我忘了提到在phpmyadmin中运行查询通常会返回结果。
I am not sure why you are iterating over 500 times? 我不确定你为什么要重复超过500次? this doesn't make sense.
这没有意义。
Best practice to retrieve the data from DB is 从DB检索数据的最佳做法是
$sql = "SELECT foo_id FROM foo_table
WHERE foo_name = 'bar'";
$res = $conn->query($sql);
if ($res->num_rows > 0) {
// output data of each row
while($row = $res->fetch_assoc()) {
echo $row["foo_id"];
}
}
OR 要么
$foo_id = ''
if ($res->num_rows > 0) {
// output data of each row
while($row = $res->fetch_assoc()) {
$foo_id = $row["foo_id"];
}
}
for ($i = 0; $i < 500; $i++) {
echo $foo_id;
}
我找到了解决方案,问题是真实的数据库在记录中使用了希腊字符而数据库必须设置为utf8-bin,这样做之后才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.