0

I'm trying to create a while loop in PHP which retrieves data from a database and puts it into an array. This while loop should only work until the array its filling contains a certain value.

Is there a way to scan through the array and look for the value while the loop is still busy?

to put it bluntly;

$array = array();
$sql = mysql_query("SELECT * FROM table");

while($row = mysql_fetch_array($sql)){

    //Do stuff

    //add it to the array

    while($array !=) //<-- I need to check the array here
}
2
  • 1
    Wouldn't it be better to restrict what the query returns i.e. SELECT * FROM table WHERE x=10 OR x=12 Commented Jul 31, 2013 at 13:46
  • 1
    fyi mysql_* functions are deprecated.. use mysqli_* or PDO Commented Jul 31, 2013 at 13:51

3 Answers 3

1

You can use in_array function and break statement to check if value is in array and then stop looping.

Sign up to request clarification or add additional context in comments.

4 Comments

@Resitive: I do hope you've read my answer, too because applying in_array to $array doesn't make sense for a number of reasons
@Resitive He's right, instead of checking array it's better to check current data row.
Thanks @ElonThan, but don't you think the other issues I mention in my answer are valid, too? using a deprecated extension is never a good idea, but what could be even better to solve the OP's problem IMHO would be using a WHERE clause, combined with ORDER BY, LIMIT and/or GROUP BY they might yield an even better sollution
@EliasVanOotegem Didn't read everything before ;) Of course you're right with everything you wrote. One thing to add is selecting only necessary columns from table.
1

First off, I think it'd be easier to check what you're filling the array with instead of checking the array itself. As the filled array grows, searching it will take longer and longer. Insted, consider:

$array = array_merge($array, $row);
if (in_array('ThisisWhatIneed', $row)
{
    break;//leaves the while-loop
}

However, if you're query is returning more data, consider changing it to return what you need, only process the data that needs to be processed, otherwise, you might as well end up with code that does something like:

$stmt = $db->query('SELECT * FROM tbl');
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    if ($row['dataField'] === 'username')
    {
        $user = $row;
        break;
    }
}

WHERE could help a lot here, don't you think? As well taking advantage of MySQL's specific SELECT syntax, as in SELECT fields, you, need FROM table, which is more efficient.
You may also have noticed that the code above uses PDO, not mysql_*. Why? Simply because the mysql_* extension Is deprecated and should not be used anymore
Read what the red-warning-boxes tell you on every mysql* page. They're not just there to add some colour, and to liven things up. They are genuine wanrings.

Comments

1

Why don't you just check each value when it gets inserted into the array? It is much more efficient than iterating over the whole array each time you want to check.

$array = array();
$stopValue = ...
$sql = mysql_query("SELECT * FROM table");

while($row = mysql_fetch_assoc($sql)){


    array_push($array,$row['column']);
    if($row['column'] == $stopValue){
          // The array now contains the stop value
          break;

}

2 Comments

mysql_fetch_array != mysql_fetch_assoc, so $row['column'] won't work
Just pointed out an omission in those edits, before somebody DV'ed you for it ;)

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.