Simple PHP script to check the MySQL database connection from web-browser

Want to verify the DB connectivity from the web browser?

This can be done from by adding a simple PHP script. See how!!

It’s very easy and interesting to check your MySQL database connectivity from your account without login as a root user. Here is a script to do the same.

To check the database connectivity, you must know the database hostname, normally it is “localhost” then database-name, username and password.

If the DB is hosted on a remote server [remote DB server], change the host to that server’s hostname. Also the connectivity from this machine to DB machine should be enabled.

PHP Code:

<?php
mysql_connect('db_host', 'db_username', 'password') or die('Could not connect the database : Username or password incorrect');
mysql_select_db('db_name') or die ('No database found');
echo 'Database Connected successfully';
?>

It’s very simple concept, first the “mysql_connect” argument will check the database hostname, username and password.

If the first argument is true, then PHP take the second line to execute else the script will die with an output given in the Die section. Similarly, mysql_select_db check the database on the server.

If both of the arguments are true then you will get an output given after the echo command.

Example:

1, If all of the input data(db_name, username, password, hostname) are right, then the output is like;

Database Connected successfully

2, If any of the entry in ‘mysql_connect’ secion is wrong, then the output is;

Could not connect the database : Username or password incorrect

3, If the database entry is wrong;

No database found

That’s it 🙂

 

Related Posts:
How to install php-pear mail on CentOS or RHEL
Managing PHP extensions (cPanel)

Post navigation

Ann Philip

I'm Ann, a freelancer in web-designing and also I have interest in blogging. I'm glad to be an editor/author in CryBit :-) Here, I will post some interesting web hosting + design related topics. Please let me know your suggestions as comment.

Leave a Reply

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