connecting to mysql using php

PHP and MySQL is one of the most preferred choice among open source web developers.  Here is PHP script to open connection to MySQL database server.

$db_hostname = "SERVER_NAME";
$db_database = "DB_NAME";
$db_username = "DB_USER";
$db_password = "DB_USER_PASSWORD";

$cnn = mysql_pconnect($db_hostname, $db_username, $db_password) or die(mysql_error());

mysql_select_db($db_database, $cnn);

if ($cnn) {
echo 'Connected successfully';
}

mysql_close($cnn);

The above script will open the connection to MySQL from PHP, then it will select the requested database. Once the connection is establisted it will print "Connected Successfully". On the last line mysql_close( link identifier) is used to close the connection.

Closing connection is not neccessary as non-persistent open links are automatically closed at the end of the script's execution. You should still use it as best practice. 


SQL Server DBA

I am a SQL Server DBA with almost 9 years of experience in database technologies.

Right now I am in process of redesigning this website in ASP.NET 2.0 and AJAX. The main purpose of new SQL DBA website is to offer more help to database professionals and make it easy for me to update contents on regular basis.