Explain PHP script and MySQL source code

From CMEE4K
Jump to navigation Jump to search

index.php

<?php
print "<!DOCTYPE html>\n<html>\n<head>\n<title>PHP with MySQL examples</title>\n</head>\n<body>\n";
print "<small><font color=grey>Caller: ".$_SERVER['HTTP_USER_AGENT']."</font></small>\n";

print "
<h3>Query programs</h3>
  <ul>
    <li><a href='queryFood.php'>Food</a></li>
    <li><a href='queryOrder.php'>Order</a></li>
    <li><a href='queryOrderFood.php'>Order and Food</li>
  </ul>
<hr><br>
<table><tr>\n";

// Each iframe can call its own web page
print "<td><iframe src='queryFood.php'      style='border:none;' height='400' width='400'></iframe><td>\n";
print "<td><iframe src='queryOrder.php'     style='border:none;' height='400' width='400'></iframe><td>\n";
print "<td><iframe src='queryOrderFood.php' style='border:none;' height='400' width='400'></iframe><td>\n";

print "</tr></table>\n";
print "</body>\n<html>\n";
?>

queryFood.php

queryOrder.php and queryOrderFood.php files have same structure, they won't be shown here.

<?php
$servername = "localhost";
$username   = "cmee4k";
$password   = "XXXXXX";
$dbname     = "cmee4k";

print "<!DOCTYPE html>
<html>
<head>
<title>PHP Query Food</title>
</head>
<body>
<h3>Food Table</h3>
";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT FoodNum, FoodName, FoodImage, FoodCustomize FROM Foods ORDER BY FoodNum";
$result = $conn->query($sql);

//DBG: print var_dump( $result );
if ( $result->num_rows > 0 ) {
   // Generate table and column header
   print "<table style='text-align:left' border='1' cellpadding='1' cellspacing='0'>\n";
   print "<tr><th>Food #</th><th>Name</th><th>Picture</th><th>Customize</th></tr>\n";

   while( $row = $result->fetch_assoc()) {
      // Generate each row and each column
      print "<tr>";
      print     "<td>".$row["FoodNum"]."</td>";
      print     "<td>".$row["FoodName"]."</td>";
      print     "<td><img src='".$row["FoodImage"]."' width='75' height='50'/></td>";
      print     "<td>".$row["FoodCustomize"]."</td>";
      print "</tr>\n";
   }
   print "</table>\n";
} else {
   print "0 results";
}
$conn->close();
print "</body>\n</html>\n";
?>