In addition to using the MySQL functions with the SQL INSERT statement to add records to a database, it is also possible to retrieve records from a database table using the SQL SELECT statement. A typical input form for selecting existing records from a Directory table is shown below. In this example a ficticious company, Company XYZ, has an online form that allows users to enter an employee's last name and search for the employee's full name, telephone number, and e-mail address.
The SQL SELECT Statement is shown below:
For more a more in depth look at the SELECT Statement see the SQL reference.
The following code is used to process the DirectorySearch.php form:
<?php
if ($_POST['submit'] == "Search")
{
//Collect Form Data
$string = $_POST['search'];
//Establish data connection
$conn = mysql_connect('localhost','root','xyxyxy');
//Select database
$db = mysql_select_db('Membership', $conn);
//Issue SQL SELECT Statement
$sql = "SELECT * FROM Directory WHERE LName = '$string'";
$rs = mysql_query($sql, $conn);
}
?>
<!DOCTYPE html PUBLIC "-//W3C/DTD/XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Web Page</title>
<style>
body {margin:15px;font:10pt Verdana}
td {vertical-align:top;border:solid 1px gray}
input,textarea{border:0px}
</style>
</head>
<body>
<form action="DirectorySearch.php" method="post">
<p>Enter a last name below and click the "Search" button to
locate employee telephone number and e-mail address</p>
<table>
<tr>
<td colspan="2">Company XYZ Directory</td>
</tr>
<td><input type="text" size="15" name="SearchName"/></td>
<td><input type="submit" value="Search" name="submit"/>
</tr>
</table>
</form>
<div>
<?php
if($_POST['submit'] == "Search")
{
while($row = mysql_fetch_array($rs))
{
echo "Name: " .$row['FName'] . " ";
echo $row['LName'] . "<br/>";
echo "Telephone: " . $row['Telephone'] . "<br/>";
echo "Email: " . $row['Email'] . "<br/>";
}
mysql_close($conn);
}
?>
</div>
</body>
</html>
This page contains two PHP code blocks. The first is executed when the "Search" submit button is clicked. The last name entered by the user is assigned to the scalar variable '$string'. Next, a connection to the database is established and an SQL SELECT statement is issued to select all (*) fields for the table records whose 'lastname' field is equal to the last name string entered by the user. Finally, the SQL statement is executed. If matching records are found the recordset is assigned to the '$rs' variable.
The second code block appears in the XHTML body section of the document. Code containing echo or print statements is normally placed between the opening and closing <body> tags so that it can be displayed or formatted in relation to other page elements. Echo and print statements appearing in PHP blocks coded above the <html> always appears at the top of the page and preceds all other page elements.
The purpose of this code block is to display the records retrieved when the SQL statement was executed in the previous code block. First, an if statement is used to determine if the $POST array variable is empty. If the array is empty this means the form has not been submitted and no records have been retrieved. If this conditional statement is omitted, errors will occur because the odbc_fetch_array will not contain any values.
Next a while loop is used to iterate through the recordset. During each iteration through the recordset, The mysql_fetch_array() function creates an associate array (here the array is called $row) containing the field values for the current record. The array indices correspond to the table field names and the array element corresponds to the value of the field. Each record is then displayed $row['FName'] - the value of the 'FirstName' field, $row['LName'] - the value of the 'LastName' field, $row['Telephone'] - the value of the 'Telephone' field, and $row['Email'] - the value of the 'Email' field. This process continues, each time the $row array containing new values, until the end of the recordset is reached.
After all records are displayed, the database connection is closed using the mysql_close() function.
Below is an example of output generated after a search:
If the user searches for a last name that does not exist in the database table, no records are displayed. To prevent confusion, the script above can be slightly modified so that a message is displayed if no matching records are found.
<?php
if($_POST['submit'] == "Search")
{
while($row = mysql_fetch_array($rs))
{
echo "Name: " .$row['FName'] . " ";
echo $row['LName'] . "<br/>";
echo "Telephone: " . $row['Telephone'] . "<br/>";
echo "Email: " . $row['Email'] . "<br/>";
}
if (mysql_num_rows($rs) == 0)
{
echo "No records found!";
}
mysql_close($conn);
}
?>
</div>
</body>
</html>
The modified script shown above adds the mysql_num_rows() function. This function requires one parameter - a reference to the current recordset $rs and determines the number contained by the recordset from last SQL SELECT operation. If the value return by mysql_num_rows() is 0, no rows were affected by the SQL SELECT statement. Therefore, no matching records were found.