Showing posts with label mysql free. Show all posts
Showing posts with label mysql free. Show all posts

Tuesday, December 18, 2012

MySQL How To : PHP For Beginners Lesson 13

MySql How To
By Stephon Unomon
Google +
Making the connection to MySQL...
 

 
Here we will go over MySql as a part of the Beginner PHP Course.  This important to know so I put together a Mysql How To

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!
 
Hope You Enjoyed This Mysql How To