By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

PHP Connect to MySQL

Quick-reference on how to connect to MySQL using PHP.

Edited: 2011-10-30 14:50

To connect to MySQL using PHP, you can use the mysql_connect function.

In the below example, variables was used for simplicity, you can easily fill them out with your own details.

Details such as which port the server is running on, is defined in the configuration. The "localhost" part can be followed by a port number. I.e. "localhost:2000".

It is also possible to type in the exact LAN Ip of the server, but usually only necessary if MySQL is running on an other computer.

$MYSQL_Host = 'localhost'; // The host to connect to, this is usually just the localhost.
$MYSQL_Uname = 'MySite_com'; // YourUserName
$MYSQL_Pword = 'MyPassword'; // YourPassWord
$MYSQL_Dbase = 'MySite_com'; // YourDatabase

$Connection = mysql_connect($MYSQL_Host, $MYSQL_Uname, $MYSQL_Pword);
$DB_selected = mysql_select_db($MYSQL_Dbase, $Connection);