Share on Google+Share on Google+

Check PHP MySQL Connectivity

Check PHP MySQL Connectivity: In this tutorial you will get to know about the connectivity of PHP and MySQL server. It also describes about the predefined objects which helps us to connect with the server.

Check PHP MySQL Connectivity:

The first step of any kind of connectivity is to check whether the connectivity has been established or not. To check the connectivity we need to know three things: The host name, Username, and respective Password.

Usuaally Host name is localhost, Username is root and password is " " (blank, unless you have set it ). After getting all these details we can connect a PHP page with MySQL server.

Following piece of code will help you to check whether your PHP web page has been connected with MySQL server or not.

Code 1:


$con=mysql_connect("localhost","root", "");



        die('could not connect'. mysql_error());




$result=mysql_query("select * from personal");


echo"<table border=1>";

echo"<tr><th>Employee ID</th><th>Address</th>";

while ($row=mysql_fetch_array($result))



        echo "<td>".$row['emp_id']."</td>"."<td>".$row['address']."</td>";

        echo "</tr>";






Employee Id
emp01 new d
emp02 new d
emp03 new d

Little descriptions about the predefined objects:

die : It is a language construct and equivalent to exit(), outputs a message and terminates the program.

mysql_connect: Open a connection to a MySQL server.

mysql_error(): Returns the error message.

mysql_select_db: Select a MySQL database

mysql_query: It is a function which is used for send a sql query.

mysql_fetch_array: Fetch a result row of a query as an associative array.

mysql_close: Close MySQL connection.




Posted on: November 2, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null