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. 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.
Edit: I forgot to mention that running the query in phpmyadmin returns results normally.
2
Answers
I found the solution, the problem is that the real database used greek characters in the records and the database had to be set to utf8-bin, after doing so, it worked.
I am not sure why you are iterating over 500 times? this doesn’t make sense.
Best practice to retrieve the data from DB is
OR