How to Test PHP MySQL Database Connection Using Script

MySQL is a popular database management system while PHP is a server-side scripting language suitable for web development; together with Apache or Nginx HTTP servers, are the different components of the LAMP (Linux Apache MySQL/MariaDB PHP) or LEMP (Linux Nginx MySQL/MariaDB PHP) stack receptively.

If you are a web developer then you might have installed these software packages or used them to setup a local web server on your system. In order for your website or web application to store data, it needs a database such as MySQL/MariaDB.

For the web application users to interact with the information stored in the database, there must be a program running on the server to pick requests from client and pass to the server.

In this guide, we will explain how to test a MySQL database connection using a PHP file. Before moving further, make sure you must have LAMP or LEMP installed on the system, if not follow these tutorials to setup.

Setup LAMP Stack on Linux Systems

  1. Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9
  2. How to Install LAMP with PHP 7 and MariaDB 10 on Ubuntu 16.10
  3. Installing LAMP (Linux, Apache, MariaDB, PHP/PhpMyAdmin) in RHEL/CentOS 7.0

Setup LEMP Stack on Linux Systems

  1. How to Install LEMP (Linux, Nginx, MariaDB, PHP-FPM) on Debian 9 Stretch
  2. How To Install Nginx, MariaDB 10, PHP 7 (LEMP Stack) in 16.10/16.04
  3. Install Latest Nginx 1.10.1, MariaDB 10 and PHP 5.5/5.6 on RHEL/CentOS 7/6 & Fedora 20-26

Quick MySQL Database Connection Test Using PHP Script

To do a quick PHP MySQL DB connection test, we will use a following handy script as file db-connect-test.php.

<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php

$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';

$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");

$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);

$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}

if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
} 
?>
Script to Test PHP MySQL DB Connection
Script to Test PHP MySQL DB Connection

Now change the database name, database user and user password as well as the host to your local values.

$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';

Save and close the file. Now run it as follows; it should print the total number of tables in the specified database.

$ php -f db-connect-test.php
MySQL DB Connection Test
MySQL DB Connection Test

You can cross check manually by connecting to the database server and listing the total number of tables in the particular database.

You may also like to check out these following related articles.

  1. How to Find MySQL, PHP and Apache Configuration Files
  2. 12 Useful PHP Commandline Usage Every Linux User Must Know
  3. How to Hide PHP Version Number in HTTP Header

Do you have any other way or script to test a MySQL DB connection? If yes, then use the feedback form below to do that.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

12 Comments

Leave a Reply
  1. This is a great script for troubleshooting purposes.

    Question: Is there any modification that can be made to it that lists the column names in a table instead of just a table count?

    Thanks in advance,

    -joey

    Reply
  2. Hey @Aaron Kili, thanks for this script it was very useful to me. Now I want to build a small PHP script about an invoice without a user interface, it should check for me how many customers in the database have paid in the database, then i should be able to put this as a service since its sending to many people.

    My question is how do i get to build this system without a user interface and and my invoice should have CSS script embedded. please help thanks

    Reply
    • @Elizabeth

      What do you want to use the CSS for if you don’t want a user interface(UI)? To the best of my knowledge, CSS is used for describing the presentation of Web pages. Trying to embed CSS means adding a UI.

      Reply
    • @UgoChukwu

      You are probably running the script from a wrong directory, check the permissions on that directory whether it allows execution of files.

      Reply
  3. This is an integration test. Next Step could be to adapt it into an automation framework (phpunit?) and then be used as part of a CI pipeline.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.