Free PHP Scripts, SEO tools and tutorials - Sim.PHP


Scripts.com

Connecting to a MySQL database from PHP

In order to pull information from a MySQL database to your php pages, you need to connect to the database first.
First, define the following variables:

<?php
$host 
"localhost"// your MySQL host i.e. the server on which the database is, usually localhost
$user ""// your MySQL username
$pass ""// your MySQL password
$db ""// the database to which you're trying to connect to
?>
                    

The connection variable ($conn) tells the script to either connect to the specified database or die if the connection could not be made (in this case, the script will display the message enclosed in the brackets).
The last line of the script selects the MySQL database and allows you to pull data from it.

<?php
$conn 
mysql_connect("$host""$user""$pass") or die ("Unable to connect to database.");
mysql_select_db("$db"$conn);
?>
                    


 
Sim.PHP © 2004 - Free PHP Scripts, SEO tools and tutorials | Privacy policy