Connect to Mysql datebase

Posted by Shek on April 14, 2008 under PHP | Be the First to Comment | Total View: 2,469 views

Editor’s note: In this post, I would like to show you how to use PHP to connect to a MySQL database. It is very a important part in the PHP and MySQL programming.

To be able to connect to the MySQL database. We have to put a set of PHP code at the beginning of the file in order to connect. The code are as below:

1
2
3
4
<?php
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed");
mysql_select_db("DataBase Name")or die("Connection Failed");
?>

The first line is to connect to the database with the mysql_connect ( ) function:

For the mysql_connect ( ) function:

  • The first parameter from the left is the host name of the MySQL database.
  • The second parameter from the left is the user name of the MySQL database.
  • The third parameter from the left is the user password of the MySQL database.

The second line is to select the database with the mysql_select_db( ) function, and the name of the database as the parameter.

The last part “or die(“Connection Failed”)” is a conditional statement which means that if the operation failed then a message “Connection Failed” should print out onto the screen.

Not what you want? Try a Search

Add A Comment

*