Connecting to the Database

Du you want to know how to enhance your website with Databases? This Tutorial is the first in a Series of Tutorials aimed at Database Applications.

The first thing we need to do before we can use MySQL, is to connect to the database. To do this, we will use something like the below code.

$MYSQL_Host = 'localhost'; // The host to connect to, usually this is the localhost.
$MYSQL_Uname = 'MySite_com'; // YourUserName
$MYSQL_Pword = 'MyPassword'; // YourPassWord
$MYSQL_Dbase = 'MySite_com'; // YourDatabase

$Connection = mysql_connect($MYSQL_Host, $MYSQL_Uname, $MYSQL_Pword);
$DB_selected = mysql_select_db($MYSQL_Dbase, $Connection);

The $Connection variable is used to refer to the connection, as we did when selecting the Database.

$DB_selected = mysql_select_db($MYSQL_Dbase, $Connection);

Selecting which database to use, makes sure that subsequent calls to mysql_query() will be performed on the active database.

We do not need to refer to the database in our queries, as the active database automatically is selected.

Post comment

Links that you insert are not nofollowed, but will be removed by admins if they are considered spam.

[url=Absolute URL for page]TITLE[/url]

You should insert code boxes around code examples, which will be automatically syntax highlighted.

[code1 html|css|javascript|php|sql]Your Code Here[/code1]

You may want to read our Privacy Policy before submitting your comment.