Skip to main content

MySQL Error Information

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.

Comments

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynam

MySQL General Architecture

        MySQL operates in a networked environment using a client/server architecture. In other words, a central  program acts as a server, and various client programs connect to the server to make requests. A MySQL  installation has the following major components: MySQL Server, Client programs and MySQL non client  utilities.  MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual  database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many  simultaneous client connections. Clients can connect via several connection protocols. For managing  database contents, the MySQL server features a modular architecture that supports multiple storage engines  that handle different types of tables (for example, it supports both transactional and non-transactional  tables). Keep in mind the difference between a server and a host. The server is software (the MySQL server  program mysqld). Server characteristi