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" <[EMAIL PROTECTED]>
To: "Sunfire" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, February 04, 2003 11:58 AM
Subject: Re: [PHP] testing if a query was successful


> --- Sunfire <[EMAIL PROTECTED]> 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
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to