PHP and MySQL Database Access



index
Disabled back button Next Section
printable version

Section 0: Module Objectives or Competencies
Course Objective or Competency Module Objectives or Competency
The student will be able to write software programs that connect to a database and issue SQL statements to manipulate that database. The student will be able to explain the concept of embedding SQL code in software applications.
The student will be able to write basic PHP applications in order to connect to and manipulate a database using PHP.
Section 1: Overview

[These notes are derived, in part, from this chapter. Please refer to the chapter for more details.]


In order to access a database, the program must first establish a connection to the database.

Once a connection has been established, the database tables can be queried.


Note: the pubs database has been created on classweb for practice accessing a remote database.

You access it using the following:

$server = 'classweb.cob.isu.edu';
$database = 'pubs';
$userid = 'databaseStudent4407';
$password = 'notAhard1';
$port = '444';

You have select permissions only.

The structure of the database can be seen here.

Section 2: Connecting to a Database
Connection Function

The mysqli_connect function provided by PHP establishes a connection to a MySQL server.

Connect to the database

There are various ways of connecting to a database. The details below pertain to a MySQL database.

A connection string can be defined as consisting of several factors, e.g. server, port, database name, user id, and user password.

mysqli_connect accepts a variety of parameters, including…

Section 3: Simple Queries

We will demonstrate a SELECT query to extract data from one of the tables in the pubs database.


Writing the Query

Recall that the SELECT command lists the table contents, and can be followed by a set of attributes to be listed.


Issuing the Query

The mysqli_query function accepts a database connection and a query string, and performs a query on the database.


Accessing the Results

Here is the code used for this version.

Handling MYSQL Query Results In PHP provides an excellent explanation of dealing with results.


Alternative Approach

Here is an alternative approach that uses the Iterator interface:

foreach ($connection->query("select title from titles;") as $row)
{
   echo $row['title'] . '<br/>';
}

See this tutorial for an additional example.

Here is the code for the alternative version.


Demos

demo page shows the results, and here is the code once again.

Section 4: Basic Demo
Using PHP to dump a MySQL File

This demo program uses PHP to retrieve and dump the content of catalog data in a MySQL database.

Here is the code for this demo.

The include files, dbDetails.inc.php and dbConnect.inc.php, are identical to the previous example with the exception that the database being used is "productcatalog".

As in the previous example, the PHP code establishes a connection to the database, issues a SQL query, and then loops through the results.

The script to create and populate the table can be found here.

Section 5: Enhanced Demo
Using PHP, JavaScript, Ajax, JSON, and jQuery to dump a MySQL File

The previous example dumps the entire product catalog, but sites like Amazon allow the user to select a product and then displays details for that product only.

This demo program uses PHP to retrieve and dump the content of catalog data in a MySQL database.

Here is the PHP code for this demo.

The html file can be viewed by right clicking on the page and selecting View Page Source, and the JavaScript/jQuery code can be viewed by clicking on "enhancedCatalogDemo.js" near the bottom of the html code.

The code for this demo is much more complex due to the interactivity required.

Section 6: Resources

Old C# Notes on Database Access



Connect Script in PHP - Mysqli_connect

Secure Your PHP Connect Script

MySQL Select Query Using PHP

Display MySQL Query Results With PHP

Display MySQL Query Results in a HTML Table with PHP

Insert into MySQL Database Table With PHP & Debugging Tips

PHP and MySQL with mysqli methods


See PDO Tutorial for MySQL Developers.