Home Tutorial Php Phpdatabase Check PHP MySQL Connectivity

 
 

Check PHP MySQL Connectivity
Posted on: November 2, 2009 at 12:00 AM
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:

<?php

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

if(!$con)

{

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

}

 

mysql_select_db("roseindia",$con);

$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"<tr>";

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

        echo "</tr>";

}

echo"</table>";

mysql_close($con);

?>

Output:

Name
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.

 

Related Tags for Check PHP MySQL Connectivity:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.