2

EDIT: Just to clarify, the query is correct - there are zero rows - the problem I am having is how to handle that in the PHP with the IF basically I am iterating through rows (10 of them) some will have some data for the query and some won't. The page falls over when a row doesn't. Does that make any sense?

I know I'm doing something inherently wrong and I am sure the solution is simple...

MySQL query brings back ZERO rows;

SELECT * from tresults WHERE date = 'MAY2012'

I then have some PHP code as follows:

if($row = mysql_fetch_array($result)){
    // do something
}

Now, as the query brings back ZERO rows, the page falls over on the IF statement...what am I doing wrong....

8
  • @Vague - yep, I'm sure it is correct - what I need to do however, is manage it so the page doesn't fall over - if that makes sense? How does one deal with this? Any suggestions? Thanks. Commented Feb 24, 2012 at 10:30
  • @Devart - the DATE field isn't the issue, the query is running correctly and as it should. The problem, I think is the way I'm handling (or not) the situation in PHP. Commented Feb 24, 2012 at 10:30
  • try SELECT * from tresults WHERE date like '%MAY2012%' Commented Feb 24, 2012 at 10:31
  • If you do not care if query returns any rows, you do not need an if statement.. Commented Feb 24, 2012 at 10:33
  • use while instead of if statement... Commented Feb 24, 2012 at 10:35

2 Answers 2

2

Check for the number of rows using mysql_num_rows()

if(mysql_num_rows($result) > 0){
  $row = mysql_fetch_array($result);
}

Or use while - note this will run for every row, if your query returns more than one row

while($row = mysql_fetch_array($result)){
  // do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @simonmayer - this is where it causes me a problem - I need it to be IF and not WHEN :( - I think I'm going to have re-code my page.
0
$result = mysql_query("SELECT * from tresults WHERE date = 'MAY2012'");

while ($row = mysql_fetch_array($result))
    {
        //do stuff;
    }

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.