How to Log MySQL Errors in PHP

If you are using PHP to access MySQL database, then there is high probability that you will be frustrated with the errors and malfunctioning of your code, without getting the source of the issues. Usually, this happens because of the error being initiated by the MySQL query, which forces the PHP to suppress the error.

Fortunately, the option for logging MySQL errors in PHP was introduced when PHP4 libraries were launched. So, you do not need to be frustrated anymore, as this article will help you in going through the right process to create a log for MySQL errors.

Instructions

  • 1

    First of all, you need to install PHP4 libraries on your computer, which will help you in creating a log for MySQL errors in PHP.

  • 2

    Once you are done with downloading and installing new libraries, you need to open the PHP file that contains the MySQL queries. For that purpose, use a plain-text editor.

    In PHP, you will find such functions beginning with "mysql_".

    For instance, if the function is a query, then it will start with "mysql_query()" or "mysql_connect()",  if it is a connection.

  • 3

    Now you need to locate the link variable that is associated with the MySQL connection.

    The link variable should look like "$link = mysql_connеct('localhost','usеrname','password');".

  • 4

    Once the link variable is located, navigate through the PHP code and find the MySQL function that you want to check for possible errors. To check the error, write "echo mysql_еrror($link);" right after the MySQL function which you selected.

    Generate the code and you will get the error that MySQL last reported. If there are no errors in the stated code, then nothing will be reported to you.

  • 5

    In case, your MySQL connection is named something other than "$link", then you should use it in the code instead.

  • 6

    Once you are done with logging the errors, save the file and exit the editor.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 − four =