2
  1. while($row=mysqli_fetch_array($result)) { echo $row['sth']; .....}

    1. $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

1
  • Something tells me that your design got something wrong. Usually I really despise queries inside a loop. What I usually do is populate what I need before (like getting a list of IDs) and then execute another query using IN (youridshere). Last but not least, if data is not extremely huge I prefer to collect it in arrays and then used in other queries and so on. PS: SANITIZE your $_POSTs too! :) Commented Dec 23, 2014 at 14:28

2 Answers 2

1

Use do-while instead of while. That way you can move the first mysql_fetch_array() call out of the loop:

<?php
    // code
    $row=mysql_fetch_array($result);
    if($row) {
        do
            echo $row["sth"];
        while($row=mysql_fetch_array($result));
    }
    //code
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is, you are always overwriting your variable inside the loop. Use it as an array.

EDIT: My fault, I've added the result as an array, not the row.

Change this:

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);
}

(added the [] after the $row, so now it's $row[]. Outside of the loop, says var_dump($row);

After this, you can set up a new foreach loop. Do not add the while loop to it.

3 Comments

This exacrly is my problem. Ill give iy a try and get back to you. Thanks!
After changing to $result[], I get mysqli_fetch_array() expects parameter 1 to be mysqli_resulterror message on while($row=mysqli_fetch_array($result)).
Check my edited post, sorry, that was my fault. I've added the result as an array, not the row. And rename the $row to $rows everywhere :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.