HOME and CODE

PHP & MySQL Home
secure.php
nav.php
notes.php
Download Tutorial

DATABASE

Create Database
Drop Database
Re-Create All

TABLE

Create Table
Drop Table
Add Record
Update Record
Delete Record
Search
View all Records

MORE PHP

PHP Console Apps
PHP Web Apps

PHP and MySQL - SELECT * FROM customer WHERE ...

$_POST[last_name] = ""

Last or First Name

 

<!-- search.php -->

<?php
  $searchText = $_POST[last_name];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META name="description" content="Tutorial for Linux, Apache, MySQL, SQL and PHP - LAMP">
<META name="keywords" content="tutorial, linux, apache, mysql, php, code, sql, lamp, lesson, lessons, example, examples, beginner, beginners, beginners' beginner's">
<title>PHP and MySQL - SELECT * FROM customer WHERE ...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table border="1" cellspacing="0" cellpadding="4">
  <tr valign="top"> 
    <td><?php require "nav.php"; ?></td>
    <td>
<h1>PHP and MySQL - SELECT * FROM customer WHERE ...</h1>
	
      <?php
        // Used for debugging only ...
        if ((isset($searchText)) && ($searchText != ""))
        {
	  echo '<p><strong>$_POST[last_name]'." = \"$searchText\"</strong></p>";
	}
	else
	{
	  echo '<p><strong>$_POST[last_name]'." = \"\"</strong></p>";
	}
      ?>
      <form name="form1" method="post" action="search.php">
        <table border="1" cellspacing="0" cellpadding="4">
          <tr> 
            <td><div align="right">Last or First Name</div></td>
            <td><input name="last_name" type="text" id="last_name" value="<?php echo $searchText; ?>"></td>
          </tr>
          <tr> 
            <td colspan="2"><div align="right">
                <input type="submit" name="Submit" value="Search">
              </div></td>
          </tr>
        </table>
      </form>
      <p>&nbsp;</p>

<?php 
  require "../secure.php";    // Contains host, username and password

  // CONNECT TO MySQL
  $link = mysql_connect($host,        // Host name
                        $user,        // User name 
                        $password)    // User password
    or die("<h2>Could not connect: </h2>" . mysql_error());

  // USE DATABASE $database
  if (mysql_select_db ($database))
  {
    // SQL SELECT QUERY - RESULTS GO INTO $rows
    $sql  = "SELECT * FROM customer WHERE last_name LIKE '%$searchText%' OR first_name LIKE '%$searchText%'";
    $rows = mysql_query($sql, $link);  // Retrieve all the rows
    echo "<p>$sql</p>";
    // DISPLAY $rows IN A TABLE WHILE THERE ARE ANY LEFT TO DO
    echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">\n";
    while ($row = mysql_fetch_array($rows, MYSQL_ASSOC)) 
    {
      // DISPLAY THE TABLE COLUMN HEADINGS (ONLY ONCE)
      if (! isset($keys))   // Test to see if it has been done already
      {
        // EXTRACT THE COLUMN NAMES FROM $row AND DISPLAY THEN IN A TABLE ROW
	$keys = array_keys($row);
	echo "<tr>\n";
	foreach ($keys as $value) 
	{
	  echo "<td><h5>$value</h5></td>\n";
	}
	echo "</tr>\n";
      }

      // DISPLAY THE ROW DATA FROM each $row IN A TABLE ROW		
      echo "<tr>\n";
      foreach ($row as $value) 
      {
	echo "<td><p>$value</p></td>\n";
      }
      echo "</tr>\n";
    }
    echo "</table>";
  }
  
  mysql_close($link);
  
  // ========================================================================
  
  dump_page(basename($_SERVER[SCRIPT_FILENAME]));  // Display the code of this page
?>

    </td>
  </tr>
</table>

</body>
</html>