0

I have a mysql query which is checking three fields. If any of them are a 1 I want it to display the text I have following. The problem I'm having is that it is only showing the first time a variable is set, it will not output the other lines. My code is

while ($row =mysql_fetch_array($cust_check)){
    if ($row['emailmatch'] == "1"){
        $output_error = 'Your email is in our database<br />';  
    }
    if ($row['phonematch'] == "1"){
        $output_error .= 'Your phone number is in our database<br />';
    }
    if ($row['addressmatch'] == "1"){
        $output_error .= 'Your address is in our database<br />';
    }
}

Then I just want $output_error to show all that have a value of 1. Can someone help? Thanks

3
  • 2
    Avoid using mysql_* if you can Commented Nov 1, 2012 at 22:47
  • Change emailmatch to also append, what happens if $output_error phonematch equals 1, addressmatch equals 1, and then lastly emailmatch equals 1? $output_error would only contain the one error message for emailmatch? Commented Nov 1, 2012 at 22:47
  • Actually, why don't you just change your SQL statement to select only the ones that equal 1 and then just display all of them instead of using your (possibly) many if statements? Commented Nov 1, 2012 at 22:49

1 Answer 1

4

If your email matches, you are resetting your $output_error variable.

$output_error = '';
while ($row =db_fetch_array($cust_check)){
    if ($row['emailmatch'] == "1"){
        $output_error .= 'Your email is in our database<br />'; 
    }
    if ($row['phonematch'] == "1"){
        $output_error .= 'Your phone number is in our database<br />'; 
    }
    if ($row['addressmatch'] == "1"){
        $output_error .= 'Your address is in our database<br />';  
    }
}
Sign up to request clarification or add additional context in comments.

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.