Re: MySQL frustration
| From: | David Robley | Date: | Tue, 18 Jul 2000 02:57:58 +0000 |
| Subject: | Re: MySQL frustration | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-7010@lists.php.net to get a copy of this message | ||
On 18 Jul, Tom Peck wrote:
> Good afternoon everyone
>
> I'm tearing my hair out over this one - it seems so simple - but it's not
> doing what it's told... All I want to do is check if $tempName exists in
> the database - and if it doesn't I want to know. BUT no matter what i have
> tried - it either doesn't think it exists when it does - or the opposite.
> This is the latest piece of code I have been trying - can anyone find any
> problems with it? It seems that $result is always true - no matter what
> $tempName is equal to. How is this possible?
>
> Thanks for any help.
>
>
> // The frustrating code
>
> $tempName = "This Person";
>
> $sql = "SELECT * FROM tblPlayers WHERE fldName='$tempName'";
> $result = mysql_query($sql);
>
> if ( $result ) {
> echo "<br> RESULT TRUE!";
> }
>
> else
>
> echo "<br> RESULT FALSE!";
> }
>
$result might equally be called $pointer_to_the_outcomeofthisqquery :-)
The mysql_query function returns a positive result identifier on
success; success meaning that a query was sent to mysql and a result
received - even if that result is "Nah, can't find that!". The only time
you'll get a false retrun from mysql_query is if the query failed for
some reason, like a syntax error in the query or lots of other problems.
What you really want to do is count the number of rows in the table that
match your query, and base your action on whether the result is zero or
not. The mysql COUNT function might do the trick.
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA