|
DATABASE
TABLE
| Create Table |
| Drop Table |
| Add Record |
| Update Record |
| Delete Record |
| Search |
| View all Records |
MORE PHP
|
PHP and MySQL - SELECT * FROM customer
SELECT * FROM customer ORDER BY last_name, first_name
<!-- viewData.php -->
|
<!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</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</h1>
<?php
require "../secure.php";
require "viewCode.php";
viewData();
dump_page(basename($_SERVER[SCRIPT_FILENAME])); // Display the code of this page
dump_page("viewCode.php"); // Display the code of "viewCode.php"
?>
</td>
</tr>
</table>
</body>
</html>
|
<!-- viewCode.php -->
|
<?php
// ========================================================================
// ===== viewCode.php =====================================================
// ========================================================================
// The viewData() function displays the SQL query results in a table
// This function extracts the column names and uses them as table headings
// It should work for any number of rows or columns
// It will not be efficient if there are many rows or columns.
// ========================================================================
function viewData()
{
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 ORDER BY last_name, first_name";
$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);
}
// ========================================================================
?>
|
|