Sample Code III: Select all records from table "destination" in database "db2_10" on Ruby (password is not shown here)
<?php
mysql_connect ("localhost", "db2_10", "password");
/* connect to Ruby ("localhost") as user "db2_10" */
mysql_select_db ("db2_10");
/* open my database "db2_10" */
$result = mysql_query ("SELECT * FROM destination");
/* Do the query. Notice that SQL query has no semi-colon. Put the outcome into $result */
if ($row = mysql_fetch_array($result)) {
/* put the first row from $result into $row. Judge whether there is any row,
if
yes, go on. If no, skip to the end. */
echo "<table border=1><tr><td>Destination
Name</td><td>street</td><td>city</td>
<td>state</td><td>zip</td><td>contact</td>
<td>phone</td><td>cost</td></tr>\n";
/* print table heading. Notice that the HTML codes are treated as a part of content.*/
do {
printf ("<tr><td>%s</td>",
$row["dname"]);
printf ("<td>%s</td>", $row["street"]);
printf ("<td>%s</td>", $row["city"]);
printf ("<td>%s</td>", $row["state"]);
printf ("<td>%s</td>", $row["zip"]);
printf ("<td>%s</td>", $row["contact"]);
printf ("<td>%s</td>", $row["phone"]);
printf ("<td>%.2f</td></tr>", $row["dcost"]);
/* print all element in this row. % is a placeholder for data. "s" for string, and "f" for float data type. $row["dname"] stands for the data for "dname" in the array. */
} while($row = mysql_fetch_array($result));
/* get the next row. If there is one more row, loop. If not, stop looping.*/
echo "</table>";
/* print the end HTML tag for table */
} else {print "Sorry, no records were found!";}
mysql_close();
/* close the connection to database */
?>
Outcome:
\n";
do {
printf ("Destination Name street city
state zip contact phone cost ", $row["dcost"]);
} while($row = mysql_fetch_array($result));
echo "";
} else {print "Sorry, no records were found!";}
mysql_close ();
?>%s ", $row["dname"]);
printf ("%s ", $row["street"]);
printf ("%s ", $row["city"]);
printf ("%s ", $row["state"]);
printf ("%s ", $row["zip"]);
printf ("%s ", $row["contact"]);
printf ("%s ", $row["phone"]);
printf ("%.2f