By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

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.

Edited: 2015-07-21 20:37

To connect to MySQL from a linux terminal, simply type the following in a terminal window:


After connecting to the database, you can use commands like the below to navigate around:

show databases;
use dbname;
show tables;

Connecting to MySQL from PHP

How to conect to MySQL from PHP depends on the method you are using, the below is a older, deprecated, way of connecting to PHP. You should therefor instead consider using the MySQLi extension.

$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.