0

Say for example this is my code:

$query = "SELECT * FROM ...";
$exec_query = mysql_query($query, $db_connect);
$fetchdetails = mysql_fetch_array($exec_query);

I am using an if condition like this:

if ($fetchdetails['field_name'] == 3) {
 do something;
}

else {
}

But in this, only 1 row is compared from the fetchdetails array. What can I do to compare each row in that array with 3? I don't want any OOP's solutions, please, I want to stick with traditional coding. Thank you.

2 Answers 2

4

You should try this:

while($fetchdetails = mysql_fetch_array($exec_query)) {
 if ($fetchdetails['field_name'] == 3) {
  do something;
 }

 else {
 }

}

That's because, with the while(...) condition, you'll iterate over all db (selected) records. The loop will stop in an automatic way as soon as mysql_fetch_array finish record.

I'll remember you that mysql_fetch_array

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. (from manual)

and that will allow "you" to stop iteration process.

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

Comments

1

If you mean that you want to compare every row of the $fetchdetails array, then

foreach($fetchdetails as $index => $value) {
    if($value == 3) { 
        // do something
    }
}

Should do it! If you want to iterate over every SQL value, see the other answer.

Comments

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.