while($row=mysqli_fetch_array($result)) { echo $row['sth']; .....}
- $row = mysqli_fetch_array($result); while($row) { echo $row['sth']; .....}
When I change first while loop to second, I get an infinite loop. Is there a way I can do fetch_array before the while loop and not get an infinite loop?
The reason I'm trying this is...:
So I have select multiple name=options[] delivered through $_POST.
Then I am using foreach($_POST['options'] as $option) to SELECT indexes whose field value matches $option. After that, if I do a while loop as in 1. from the above, I only get the elements of the last options[].
So I want to append the results of mysqli_fetch_array to an array $row and then do a while loop as in 2. But I get an infinite loop like said.
I know my explanation is a bit confusing.... If it isn't clear up to this point, read more below. I am explaining in more details with more codes provided.
========================================================================
if(isset($_POST['options'])) {
foreach($_POST['options'] as $option) {
$query = "SELECT * FROM users WHERE stdYr = '$option'";
$result = mysqli_query($db_conx, $query) or die("Error in ".$query."<br>".mysqli_error($db_conx));
}
}
}
while($row=mysqli_fetch_array($result)) {}
With this code, when an array options[option1, option2, option3] is passed, for example, the result only outputs the elements of options[option3]. Query output of option1 and option2 is overwritten in foreach loop.
So I came up with an attempt to declare fetch_array inside foreach loop and append in to $result like this:
========================================================================
if(isset($_POST['options'])) {
foreach($_POST['options'] as $option) {
$query = "SELECT * FROM users WHERE stdYr = '$option'";
$result = mysqli_query($db_conx, $query) or die("Error in ".$query."<br>".mysqli_error($db_conx));
$row.=mysqli_fetch_array($result)
}
}
}
while($row) {}
BUT this results in infinite loop.....
Can someone help? Let me know if this doesnt make sense... Thanks ! :D