How to Troubleshoot Mysqli_connect

Troubleshooting a problem with "mysqli_connect" is important so you can successfully connect to the database and process data for your script. The "mysqli_connect" is a PHP function to connect to a MySQL database using the "mysql improved extension." Troubleshooting a non-working connection involves setting up the correct error handling code, looking up any error numbers in MySQL's appropriate header files and then fixing the specific error.

Instructions

    • 1

      Open your PHP file in a text editor, such as Windows Notepad.

    • 2

      Call the "myqli_connect" function to connect to a MySQL database and check for errors by adding the following code in the body of your PHP file:

      <?php

      $my_link = mysqli_connect('127.0.0.1', 'username', 'password', 'db', 3306);

      if (!$my_link)

      {

      die('mysqli_connect error (' .mysqli_connect_errno(). ')' .mysqli_connect_error());

      }

      echo 'Connection was successful' .mysqli_get_host_info($my_link);

      ?>

      A common mistake is to send the port as a string, which will not work as it needs to be an integer. Another mistake is to specify a non-default port with a host of "localhost." Use '127.0.0.1' instead of 'localhost' when you need to specify a port number. If you get the error message "Can't connect to MySQL server on 'localhost' (10061)" with a pipes and socket connection, try sending the host name as '.' and the socket as 'mysql', which is the default name. Note the error number in any error messages.

    • 3

      Open the MySQL "errmsg.h" and "mysqld_error.h" header files in a text editor, such as Windows Notepad to look up descriptions for any client or server error messages by their error number. If you have the MySQL source distribution, then open the "Docs/mysqld_error.txt" file to find a complete list of error numbers and messages.

    • 4

      Save the PHP file and load it on your server to investigate the "mysqli_connect" errors.

Tips & Warnings

  • Enclose all PHP code within "<?php" and "?>" tags.

Related Searches:

References

Comments

Related Ads

Featured