Connecting to a Database

MySQL is a commonly used database with PHP because it is free and open-source, like PHP. For the purposes of this section, I will assume you have a working MySQL database set up. Look in the more details section to click a llink on how to set up a MySQL database if you do not have one.

To do anything with MySQL within PHP, use the commands that come with the PHP module. To connect to a darabase, use the mysql_connect command and then the mysql_select_db command. Example:

$db_host =";
$db_name = "db1";
$db_user = "username";
$db_password = "password";
$db_connection = mysql_connect($db_host, $db_user, $db_password);
$db_select = mysql_select_db ($db_name);

1. The host is the URL of where you database is located. If you are using one supplied by your wen host, this should be given when you create the MySQL database.
2. The database name, user name and password are created when you create the database.
3. The mysql_connect command creates a data connection to the host and then mysql_select_db selects the particular database. Both commands are needed before you can do anything with the data in your database.

Next article: HTML forms

Working with tables

To change the data in the tables in a database, there are three basic steps:
1. Create a SQL query
2. Execute the query
3. Work with any results from the query
Valid SQL queries are detailled in the link given in the more detail section. As a first example, let's look at creating a table.

$query = "CREATE TABLE users (user_name TINYTEXT, user_email TINYTEXT, user_password TINYTEXT user_datereg DATETIME)";
$result = mysql_query ($query);
$error = mysql_error();
$errorno = mysql_errno();

1. $result is set to true if the query runs OK. It is set to false if there is any problem with the query.
2. $error is set with the MySQL error text if the query fails. $errorno is set with the MySQL error number if the query fails.
3. Your program needs to handle what to do if the query fails. The program continues to run irrespective of what happens to the query.

The other technique commonly used is to handle the results of a SELECT query. Here is an example using the table created above:

// Select all users who begin with 'A'
$db_connection = mysql_connect($db_host, $db_user, $db_password);
$db_select = mysql_select_db ($db_name);
$searchquery = "SELECT * FROM users WHERE user_name LIKE 'A%'";
$searchresult = mysql_query ($searchquery);
// If any results were found
if ($searchresult) {
   // Loop through results returned from the query
   while ($row = mysql_fetch_array($searchresult, MYSQL_NUM)) {
       $username = row[0];
       $useremail = row[1];
       $userpassword = row[2];
       $userdate = row[3];
       ... do what you want with the fields

Next article: HTML forms


The previous article tells you how to use files in PHP.


The next screen gives you a tutorial on how to use HTML forms.
This tutorial tells you how to create a from, call a PHP program using the form and how to process data entered in the form.

External Links


  • Create an array using the key values and a value for each element
  • Add to the end of a numeric array using []
  • Retrieve an element in an array using its key
  • Use count to find out how many elements are in an array