|
Posted by Kim Andrι Akerψ on 10/17/06 14:02
davek wrote:
> I have a diary page listing social events in a table (stored in a
> mysql db). If there are no current listings, I want to display a
> message rather than an empty table.
>
> This is the code I'm using:
> **********
> $query = @mysql_query('SELECT COUNT(*) FROM socials');
> $count = @mysql_num_rows($query);
>
> if ($count<1) {
> echo ('<p>There are no forthcoming social events.</p>');
> } else {
> //show social events
> }
> **********
>
> Unfortunately, this returns 1 when there are no results. It also
> returns 1 when there is one result.
>
> I'm probably being really dense and missing something obvious, but is
> there a way to make it return 0 when there are no results? Or is there
> a better approach to this task?
mysql_num_rows() returns the number of rows in the result, and since
you asked for COUNT(*) in your query, the result contains 1 row, and
that is the actual count.
What you want in this case, is something like this:
$count = @mysql_result($query,0);
--
Kim AndrΓ© AkerΓΈ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Navigation:
[Reply to this message]
|