MySQL connect (PHP)

by spyka (http://www.spyka.net)

This code shows you how to connect to a MySQL database and displays an error message if connection fails. Change user/password/db to your info.

The mysql_host will usually be localhost but check with your hosting provider.

Code:
<?
// Connect
$con mysql_connect("mysql_host","mysql_username","mysql_pass") or die(mysql_error());
$db mysql_select_db("db_name") or die(mysql_error());

// all connected - do SQL stuff

// close connection
mysql_close($con);
?>

Code demo

Not available

Help and comments

Discuss this and other scripts in our webmaster foruns

Learn more about our forums