CMS takes the work out of building interactive websites.

Create a website at Omnis Network using Joomla, PostNuke or other popular software.

$5.95 Web Hosting
Installs instantly using the FANTASTICO script installer!!

Your site will have automatic features like member sign up and site search.

Features like message boards and chat are easily added.

PHP MySQL
Interactive Website Design

Displaying the Data

In the script below we'll see how to use the mysql_query function to retrieve records or rows from our birthdays table.

We'll also be introduced to 2 new mysql functions num_rows and fetch_row.

Most of the script is a repetition of syntax you've already learned. The standard procedures of connecting to the server and selecting the database will not be explained.

The first line of code we'll look at (excluding the error trap) uses the mysql_query function to SELECT all rows and fields from the table birthdays. The asterick is used as a wild card. The data retrieved is stored in the variable $result.

$result = mysql_query( "SELECT * FROM birthdays" )

If we wanted to retrieve only the names from our database we would change the code accordingly to:

$result = mysql_query( "SELECT name FROM birthdays" )

The next line of code for interpretation uses a new mysql function: mysql_num_rows(). This function simply returns the number of rows or records that have been added to the birthdays table. Notice that it accesses the information retrieved by the previous mysql_query stored as $result.
$num_rows = mysql_num_rows($result);

The line following this syntax in the script prints the result to the screen.


Embedded in the code that prints the results in table form we find another new mysql function. The mysql_fetch_row() function grabs an individual record or row from $result and divides it into the original fields. (id,name,birthday)

$get_info = mysql_fetch_row($result)

A foreach loop is used to print the fields in the cells of our table.

foreach ($get_info as $field)

This script can be used to return the rows of any table or database. Just replace the database name and table name with the appropriate information. The information display will resemble the table below the code.

<html>
<head><title>(Title Here)</title></head>
<body>
<?php
$db="mydatabase";
$link = mysql_connect("localhost");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result = mysql_query( "SELECT * FROM birthdays" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
mysql_close($link);
?>
</body>
</html>
1 Peggy June4
2 Mark Mar27

Download the Scripts

The Birthdays Database management files can be downloaded in a zip file. The package contains an integrated db management system, with a simple login file and interface. To run the scripts on your PC you must have a localhost server installed along with PHP and the MySQL server.
Download birthdays_db.zip

MySQL Tutorial

To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com

Adding data Top Recommended $5.95 Hosting Editing data
PHP MySQL is a service provided by Net Success 2000 Plus Inc
PO Box 1508
Somerset, KY 42502
Last Modified: July 2, 2007

| HTML TOC | Web Design | Create a Website |

PHP MySQL - Interactive Website Design Copyright © 2007