Difference between revisions of "PHP112"

From mi-linux
Jump to navigationJump to search
Line 1: Line 1:
 +
[[Main Page]] >> [[CP2228|Interactive Systems Development]] >> [[PHP|Workbook]] >> xxx
 +
 
= A Simple HTML/PHP/MySQL Example =
 
= A Simple HTML/PHP/MySQL Example =
  

Revision as of 16:38, 27 July 2009

Main Page >> Interactive Systems Development >> Workbook >> xxx

A Simple HTML/PHP/MySQL Example

The following example will:

  1. use a form to gather criteria for a search
  2. pass the criteria to a PHP script
  3. build a custom query
  4. execute the query against a database, returning results
  5. output the results

1. A form to gather criteria (part1.html)

<HTML>
  <HEAD>
   <TITLE>Part 1 - Form</TITLE>
  </HEAD>
  <BODY>
   <FORM action="part2.php" method="post">
    <P>
     Enter pet's name: <INPUT type="text" name="searchName">
    </P>
    <P>
     <INPUT type="submit" value="Search">
    </P>
   </FORM>
  </BODY>
 </HTML>

2. The PHP script (part2.php)

<?
    $dbServer=mysql_connect("localhost","cp1079_student","");
    if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }
    
    mysql_select_db("cp1079",$dbServer);
    
    $sql ="SELECT * FROM pet";
    $sql.=" WHERE name=\"".$_POST["searchName"]."\"";  // the space before the WHERE is critical
    
    $queryResult=mysql_query($sql);
    
    if (mysql_error())
    {
      echo "Problem with Query<BR>";
      echo "The following error message was returned from MySQL:<BR>";
      echo mysql_error();
      exit;
    }
    
    if (mysql_num_rows($queryResult)==0)
    {
      echo "No pets with that name.";
    }
    else
    {
      while ($dbRecord=mysql_fetch_array($queryResult))
      {
        echo "found: ".$dbRecord["name"].", ".$dbRecord["species"].", ".$dbRecord["owner"].", ".$dbRecord["age"]."<BR>";
      }
    }
 ?>

Deconstruction of the example

part1.html

  • setting up the form to direct to part2.php on submit, and pass input fields using POST
<FORM action="part2.php" method="post">
  • creating a text field to allow input, and naming the field searchName - this is the name you need to use in part2.php
<INPUT type="text" name="searchName">

part2.php

  • connecting to the database and checking that there were no errors in the connection
$dbServer=mysql_connect("localhost","cp1079_student","");
if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }
  • choosing which database to use on the MySQL server
mysql_select_db("cp1079",$dbServer);
   
  • constructing the SQL statement, with a WHERE clause to filter the records to just those with the name we choose in part1.html, remembering that searchName was the name of the text field in the HTML file, and was sent using POST, so in the PHP file it is referenced by $_POST["searchName"]
$sql ="SELECT * FROM pet";
$sql.="WHERE name=\"".$_POST["searchName"]."\"";
   
  • execute the query and store the returned results
$queryResult=mysql_query($sql);
   
  • check for any errors when trying to execute the query, and output the error message if any has been sent by MySQL to PHP
if (mysql_error()) { ...echo mysql_error();}
   
  • at this point, the query was successful, but we don't know if any records matched the name we keyed in, so count the number of records that MySQL sent to PHP that match the criteria we entered. if records=0, output a suitable message
if (mysql_num_rows($queryResult)==0)
{
  echo "No pets with that name.";
}
  • if more than 0 records were returned, loop through them in case there is more than one and print them out. This will still work if only one record is returned.
else
{
  while ($dbRecord=mysql_fetch_array($queryResult))
  {
    echo "found: ".$dbRecord["name"].", ".$dbRecord["species"].", ".$dbRecord["owner"].", ".$dbRecord["age"]."
";

Ready to move on?

PHP113 Cookies is the next section...