Hi Chris

> I have delete working perfectly by doing the following:
> include("connectionstart.php");
> mysql_query ("DELETE FROM emaillist WHERE EMail = '$email'
> ");
> However, how do I return a true or false flag whether the row was
deleted or
> not?  I basically need to know how to do a 0 or 1 I guess once the row
> been deleted or if the specified email address wasn't found.

The manual is your friend
There are a couple of reasons why a query/DELETE might fail.
Most command checking code failure occurs because people fail to
appreciate that the return-value from mysql_query() takes advantage of
PHP's loose (data) typing, and can be a boolean value sometimes, and a
resource 'pointer' at others.
The next 'hole' to fall into is the correctness of the syntax of the
query, or that the connection between PHP and MySQL has been broken/not
established - illustrated in the manual ref given.
The second 'level' of the game is (as you ask) to check that MySQL was
actually able to do what was instructed. For this you need
mysql_affected_rows() (manual:

Here's a particularly pedantic (but hopefully illustrative)piece of code
for you:
   if ( DEBUG ) echo "<br>Query=$SQLquery~";
 $bValidity       = $MySQLResultSet
                    = @mysql_query( $SQLquery, $MySQLConnection );
 $iNumRowsDeleted = mysql_affected_rows( $MySQLConnection );
 if ( MYSQL_DEBUG ) echo "<br>Result=$MySQLResultSet~ Num
 if ( FALSE == $bValidity )
  MySQLdbError( $MySQLConnection,
        "Error: MySQL DELETE syntax/semantic/privilege error" );
 return $bValidity;

- the DELETE is performed within a function
- DEBUG and MYSQL_DEBUG are script-global constants set/reset according
to taste
- MySQLdbError() logs any errors, in or out of user view, also according
to taste
- note that the query is error-suppressed and its result assigned twice
to vars of different types (by naming convention if not PHP construct)
- the function returns a 'worked' or 'didn't' (boolean) value
- other values are returned in the function parameter/argument list, eg
the number of rows deleted
- the mainline can call the function within an IF statement
- the IF-valid check can then proceed to inspect $iNumRowsDeleted to
ensure expected behavior

Other refs: a number of the tutorials linked from MySQL AB's site cover
this topic area.


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

Reply via email to