my query is
$result=SELECT MAX(surveyID) as surveyID FROM `sg_count`WHERE userID=$user
where sg_count
table has no records
but mysql_num_rows($result)
returns 1.
my query is
$result=SELECT MAX(surveyID) as surveyID FROM `sg_count`WHERE userID=$user
where sg_count
table has no records
but mysql_num_rows($result)
returns 1.
Aggregating functions like max
and count
will return one row (without a grouping clause), that's their nature.
By way of example, count(*)
on an empty table will give you one row with the value 0
.
I suspect, although I haven't tried, that you'll be getting a single row in your query with a NULL
value. That's how all the other DBs I've used handle it. Assuming the column is non-NULL specified, you can use that return value to detect the "no row" case. Even if it allows NULLs, that may still be workable, it depends on what you want to do in that case.
Otherwise, you're probably better off checking the count as well with something like:
select count(*) as quant,
max(surveyID) as surveyID
from sg_count
where userID = 'somebody'
Then, if quant
is zero, you know there were no rows. If it's non-zero, there were rows and the maximum value of surveyID
is in the other column.