Troubleshooting or Testing the PHP Connection to MySQL Database

October 19th, 2011 by Regaz

There are two PHP to MySQL Connection functions:

  • The Procedural function: mysql_connect
  • The Object Oriented function: mysqli_connect

Not Every Host Successfully Support a Version of PHP/MySQL that Permit you to use Both Methods…
So on a Remote Host you need to check which Versions of PHP and MySQL are Supported for to know after which methods of Connection you can Use.

  1. Test MySQL Connection by mysql_connect function
    Just Insert in your PHP Script

    ?>DEFINE('DB_USER','yourUserName');
    DEFINE('DB_PASSWORD', 'yourMySQLPass');
    DEFINE('DB_HOST', 'yourMySQLHost');
    DEFINE('DB_NAME', 'yourMySQLDbName');

    $dbc = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

    if (!$dbc) {
    die('Connect Error (' . mysql_errno() . ') ' . mysql_error());

    $db_selected = mysql_select_db(DB_NAME);

    if (!$db_selected) {
    die ('Can't use' . DB_NAME . ': ' . mysql_error());
    }

    echo 'Success... Your connection to the Database is working';
    }
    ?>

    If you have some Error in Connection this will be directly displayed on the Page.

  2. Test MySQL Connection by the mysqli_connect function

    ?>DEFINE('DB_USER','yourUserName');
    DEFINE('DB_PASSWORD', 'yourMySQLPass');
    DEFINE('DB_HOST', 'yourMySQLHost');
    DEFINE('DB_NAME', 'yourMySQLDbName');

    $link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

    if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
    . mysqli_connect_error());
    }

    echo 'Success... ' . mysqli_get_host_info($link) . "n";

    mysqli_close($link);
    ?>

    In case of Successful Connection you will see a message of Success…

(Visited 1 times, 1 visits today)

Tags: ,