[‘
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.
n
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.
n
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.
n
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.
n
Setup LAMP Stack on Linux Systems
n
- n
- Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9
- How to Install LAMP with PHP 7 and MariaDB 10 on Ubuntu 16.10
- Installing LAMP (Linux, Apache, MariaDB, PHP/PhpMyAdmin) in RHEL/CentOS 7.0
n
n
n
n
Setup LEMP Stack on Linux Systems
n
- n
- How to Install LEMP (Linux, Nginx, MariaDB, PHP-FPM) on Debian 9 Stretch
- How To Install Nginx, MariaDB 10, PHP 7 (LEMP Stack) in 16.10/16.04
- Install Latest Nginx 1.10.1, MariaDB 10 and PHP 5.5/5.6 on RHEL/CentOS 7/6 & Fedora 20-26
n
n
n
n
Quick MySQL Database Connection Test Using PHP Script
n
To do a quick PHP MySQL DB connection test, we will use a following handy script as file db-connect-test.php
.
n
<?phprn# Fill our vars and run on clirn# $ php -f db-connect-test.phprnrn$dbname = 'name';rn$dbuser = 'user';rn$dbpass = 'pass';rn$dbhost = 'host';rnrn$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");rnmysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");rnrn$test_query = "SHOW TABLES FROM $dbname";rn$result = mysqli_query($link, $test_query);rnrn$tblCnt = 0;rnwhile($tbl = mysqli_fetch_array($result)) {rn $tblCnt++;rn #echo $tbl[0]."<br />\n";rn}rnrnif (!$tblCnt) {rn echo "There are no tables<br />\n";rn} else {rn echo "There are $tblCnt tables<br />\n";rn} rn?>rn
n

n
Now change the database name, database user and user password as well as the host to your local values.
n
$dbname = 'name';rn$dbuser = 'user';rn$dbpass = 'pass';rn$dbhost = 'host';rn
n
Save and close the file. Now run it as follows; it should print the total number of tables in the specified database.
n
$ php -f db-connect-test.phprn
n

n
You can cross check manually by connecting to the database server and listing the total number of tables in the particular database.
n
You may also like to check out these following related articles.
n
- n
- How to Find MySQL, PHP and Apache Configuration Files
- 12 Useful PHP Commandline Usage Every Linux User Must Know
- How to Hide PHP Version Number in HTTP Header
n
n
n
n
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.
n
‘]