Re: testing if a query was successful
| From: | Kevin Stone | Date: | Tue, 04 Feb 2003 19:38:13 +0000 |
| Subject: | Re: testing if a query was successful | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-134318@lists.php.net to get a copy of this message | ||
As quoted from the manual: "A non-FALSE return value means that the query
was legal and could be executed by the server. It does not indicate anything
about the number of rows affected or returned. It is perfectly possible for
a query to succeed but affect no rows or return no rows."
Always test your query with the assumption that you will return a non-FALSE
value. This is an important lesson to learn as it is more robust and can
save you from quite a bit of frustration. For example..
$result = mysql_query("SELECT... ") or die("Invalid query: " .
mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "Query executed but no rows were selected.";
exit;
}
$result = mysql_query("UPDATE or INSERT or DELETE... ") or die("Invalid
query: " . mysql_error());
if (mysql_affected_rows() == 0)
{
echo "Query executed but no rows were affected";
exit;
}
- Kevin
----- Original Message -----
From: "Chris Shiflett" <shiflett@php.net>
To: "Sunfire" <rusty_81@msn.com>; <php-general@lists.php.net>
Sent: Tuesday, February 04, 2003 11:58 AM
Subject: Re: [PHP] testing if a query was successful
> --- Sunfire <rusty_81@msn.com> wrote:
> > how would you use an if..else statement to test a
> > query in mysql to see if it was successfull or not?
>
> Depends on what database server you are using. Here it is
> in MySQL:
>
> if (mysql_query($sql))
> {
> echo "Success";
> }
> else
> {
> echo "Failure";
> }
>
> Chris
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>