Connecting to mySQL

Making a connection

Just like files, we need to first establish a connection to the database and get a handle to use:

  $linkID = mysql_connect("hostname", "userName", "password");  

mysql_connect needs 3 arguments:

  1. host: The host (or server) where the database server is located. This will usually be "localhost" for the local machine.
  2. userName: The username you use to access the database server.
  3. password: The password you use to access the database server.

Now that we have a handle to the database () we can do a few things:

  1. List the databases available to us with the mysql_list_dbs($linkID) command.
  2. Select a database to use with the mysql_select_db("databaseName", $linkID) command.

The first of these is usually for checking permissions and general information. For the most part we will be working with mysql_select_db("dbName",) in this class.

So, to establish a connection and select a database, our php looks like this...

 
<?php
$linkID = mysql_connect("hostname","username","password");
mysql_select_db("myDatabaseName",$linkID);
?>;
 
NEXT
PREVIOUS
Master Index