Databases Reference
In-Depth Information
or INSERT , do not return a result set. For these, we have another pair of functions that
we can use.
The mysql_affected_rows( ) and mysqli_affected_rows( ) functions report the number
of rows affected by queries that change data, such as INSERT , UPDATE , and DELETE . If no
rows were actually changed by a query, these functions return zero, but this doesn't
mean an error has occurred. For example, a zero is returned if a DELETE query with a
WHERE clause doesn't match any rows, or if an UPDATE doesn't require any values to be
changed. If an error does occur, the function returns the value -1. You can add code
to check for these return values and handle any problems.
A REPLACE query updates an existing row or inserts a new row in a table. If there is an
existing row in the table with the same key, that row is deleted before the new row is
inserted. The insertion counts as one row affected, and a deletion would count as an-
other affected row. Hence, a REPLACE query may be reported as affecting one or two
rows.
Handling MySQL Errors
We provided an introductory discussion of PHP error handling earlier in “Handling
Errors in PHP.” In this section, we take a detailed look at the classes of problems that
can occur when you're developing a script that works with MySQL. Errors that occur
in PHP scripts could be related to general PHP issues, the PHP MySQL functions, or
MySQL data:
General PHP issues
These include syntax errors in scripts, problems with the script engine, runtime
errors, and programmer-triggered errors. In turn, these are divided into the insig-
nificant NOTICE class, the significant WARNING class, and the critical ERROR class.
For example, if you leave out a quote in a PHP statement:
echo "This is output;
a PHP error message such as:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in
/var/www/html/test.php on line 22
is displayed when the page is loaded in a web browser. These problems aren't
related to MySQL and are usually related to mistyped statements or flawed pro-
gram logic.
PHP MySQL functions
These errors can occur during many operations; for example, the MySQL server
might be unavailable, it might not be possible to establish a connection because
the authentication credentials are incorrect, or an SQL query might be incorrectly
formed. These are not PHP errors, but PHP can report them.
 
Search WWH ::




Custom Search