First off, we need to be able to tell PHP to connect to a database before we can use it.
The command is straight forward - mysql_connect().
<?php
$dbhost = "localhost"; // the db’s server
$dbname = "mysite_dbname"; // the db’s name
$dbuser = "mysite_dbuser"; // the db’s username
$dbpass = "password"; // the password for the user
mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //connect to db or show error if failure
echo "Connected to database";
?>
Looking at the above code, everything is pretty self explanatory...except the “or die” thing.
This is a method of handling errors.
If for some reason PHP cannot connect to the database, the “or die(mysql_error())” part tells PHP to show us exactly what the error was.
Most of the time it is human error - the wrong username, password, etc. was entered in the code. Check your work!
0 Comments
Leave a Reply. |
David Rentson
If this free PHP and MYSQL Tutorial is helpful for you... Great! Archives
October 2008
Categories
All
|