You are here: Re: why does mysql COUNT return 1 when there are no results? « All PHP « IT news, forums, messages
Re: why does mysql COUNT return 1 when there are no results?

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]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация