1

I have the following:

SELECT q25, COUNT( q25 ) AS count, AVG (q1) AS q1
FROM tresults WHERE id = 'Yes' AND date = 'MARCH2010' AND q25 = '1' 
GROUP BY q25

At the moment, the query returns MySQL returned an empty result set (i.e. zero rows). which is correct - is it possible to get it to return NULL instead?

OR

Is there a way of dealing with this after the event in PHP, such as:

$resultprev = mysql_query($queryprev);
if($resultprev == ''){
// do something
}
3
  • That sounds like a phpMyAdmin message. Where do you need the null exactly? Commented Feb 24, 2012 at 9:56
  • I need this otherwise other code doesn't run correctly. I need the null under 'q1'. Alternatively, can this be dealt with after, see the edit above. Thanks. Commented Feb 24, 2012 at 9:58
  • 2
    php.net/manual/en/function.mysql-num-rows.php Commented Feb 24, 2012 at 9:58

2 Answers 2

3

use mysql_num_rows :

$resultprev = mysql_query($queryprev);
$num_rows = mysql_num_rows($resultprev);
if($num_rows == 0){
   // 0 results !
}

or you could union a null row :

SELECT q25, COUNT( q25 ) AS count, AVG (q1) AS q1
FROM tresults WHERE id = 'Yes' AND date = 'MARCH2010' AND q25 = '1' 
GROUP BY q25

UNION

SELECT null, null as count, null as q1
Sign up to request clarification or add additional context in comments.

1 Comment

Answers the literal question, but seems rather convoluted for what the OP actually needs.
2

try this

if(mysql_affected_rows($res)) {
    //your stuff
}

or in sql

SELECT (
    SELECT q25, COUNT( q25 ) AS count, AVG (q1) AS q1
    FROM tresults WHERE id = 'Yes' AND date = 'MARCH2010' AND q25 = '1' 
    GROUP BY q25

    UNION

    SELECT null, null, null

) AS x
LIMIT 1

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.