Home : Advice : <?PHP ?> Tutorials : Connecting to Database


In this tutorial you will learn how to connect to a MySQL database with PHP.

<?php

// database variables

$localhost = "localhost";
// database name
$database = "database name";
// username
$db_user = "username";

// password
$db_pass = "password";

// No need to edit below here

// Now connect to MySQL database

$result = mysql_pconnect("$localhost", "$db_user", "$db_pass");

if (!$result)

// If connection fails then notify
echo"could not connect to database.";

// Now select the database you will use

if (!mysql_select_db("$database"))

// If no database to select then notify
echo"could not select database.";


?>

To connect to a MySQL database you need a username, password, and the database name you want to connect to.

 


Copyright 2004 - 2005 FREEWEBHOSTINGADVICE.COM Contact | Privacy Policy & Legal