Even though developers (most at least) strive for an application free of bugs, no application (except minor
projects) can be expected to be completely bug-free. For that purpose, a good practice for programmers is
to build in code that will trap and display errors in a way that is both meaningful and useful to assist in the
correction of the error. PHP offers two functions that can display error messages from MySQL:
• mysql_error() - This function displays the error message (if there was one) from the last MySQL
function. The language that the message will be displayed in is based on the target language that is
setup in the MySQL server. However, even if a fluent speaker of the message language were to read
the majority of error messages returned, they would most likely still have a difficult time
understanding what was said. Needless to say, error messages from MySQL are not very verbose or
informative.
<?php
// Load variables used in mysql_connect and connect to server
include "connect_info.php";
$linkID1 = mysql_connect($dbhost,$dbuser,$dbpass) or
die("Could not connect to MySQL server");
// Insert a record in the INFORMATION_SCHEMA database
$query = "INSERT INTO INFORMATION_SCHEMA.TABLE SET TABLE_NAME = 'dog'";
$query_result = mysql_query($query,$linkID1);
if ($query_result) {
print "<p>INSERT successful</p>";
} else {
print "<p>".mysql_error($linkID1)."</p>";
}
?>
The response which is displayed is not exactly accurate; "Access denied for user 'root'@'localhost'
to database 'INFORMATION_SCHEMA'". The real error is that the INFORMATION_SCHEMA
database is a made up of system view tables that can only accept SELECT statements against the data
contained. The rights a user has does not control if an INSERT can be made against the
INFORMATION_SCHEMA database. This is one of many examples of error messages that are not
as accurate (or descriptive) to really be helpful. Other solutions should be considered in providing
more accurate feedback when errors occur, such as:
o Labeling code snippets and then using that naming convention in the error message to pinpoint
exactly where the error took place in the code.
o Label the error message with the line of code on which it occurred (or at least where the error
message was captured).
o Set up a function that will e-mail the results of errors to the administrator of the code or store
them in a file.
o Producing a generic error message that tells the end user there was a problem and providing
contact information for them to contact the administrator of the system.
• mysql_errno() - This function is identical to the mysql_error() function with the exception that only
the associated error number is displayed without any explanatory text. The resulting number
produced is MySQL-specific and is not portable to other database systems. Developers can use these
numbers to create customer error messages. The complete list of MySQL numeric error values can be
found at http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html.
projects) can be expected to be completely bug-free. For that purpose, a good practice for programmers is
to build in code that will trap and display errors in a way that is both meaningful and useful to assist in the
correction of the error. PHP offers two functions that can display error messages from MySQL:
• mysql_error() - This function displays the error message (if there was one) from the last MySQL
function. The language that the message will be displayed in is based on the target language that is
setup in the MySQL server. However, even if a fluent speaker of the message language were to read
the majority of error messages returned, they would most likely still have a difficult time
understanding what was said. Needless to say, error messages from MySQL are not very verbose or
informative.
<?php
// Load variables used in mysql_connect and connect to server
include "connect_info.php";
$linkID1 = mysql_connect($dbhost,$dbuser,$dbpass) or
die("Could not connect to MySQL server");
// Insert a record in the INFORMATION_SCHEMA database
$query = "INSERT INTO INFORMATION_SCHEMA.TABLE SET TABLE_NAME = 'dog'";
$query_result = mysql_query($query,$linkID1);
if ($query_result) {
print "<p>INSERT successful</p>";
} else {
print "<p>".mysql_error($linkID1)."</p>";
}
?>
The response which is displayed is not exactly accurate; "Access denied for user 'root'@'localhost'
to database 'INFORMATION_SCHEMA'". The real error is that the INFORMATION_SCHEMA
database is a made up of system view tables that can only accept SELECT statements against the data
contained. The rights a user has does not control if an INSERT can be made against the
INFORMATION_SCHEMA database. This is one of many examples of error messages that are not
as accurate (or descriptive) to really be helpful. Other solutions should be considered in providing
more accurate feedback when errors occur, such as:
o Labeling code snippets and then using that naming convention in the error message to pinpoint
exactly where the error took place in the code.
o Label the error message with the line of code on which it occurred (or at least where the error
message was captured).
o Set up a function that will e-mail the results of errors to the administrator of the code or store
them in a file.
o Producing a generic error message that tells the end user there was a problem and providing
contact information for them to contact the administrator of the system.
• mysql_errno() - This function is identical to the mysql_error() function with the exception that only
the associated error number is displayed without any explanatory text. The resulting number
produced is MySQL-specific and is not portable to other database systems. Developers can use these
numbers to create customer error messages. The complete list of MySQL numeric error values can be
found at http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html.
Comments
Post a Comment