346
Chapter 6
...at a glance
PHP
While you’ve been thinking about Katie’s JavaScript and HTML, her server-side guys
have been listening in. They’ve gone ahead and updated the getUpdatedSales.php script
to return the XML we’ve been talking about, complete with updated totals for board, boot,
and binding sales. Not too bad, huh? Here’s what the script looks like now:
<?php
// Connect to database
$conn = @mysql_connect(“mysql.headrstlabs.com”,
“secret”, “really-secret”);
if (!$conn)
die(“Error connecting to MySQL: ” . mysql_error());
if (!mysql_select_db(“headrst”, $conn))
die(“Error selecting Head First database: ” . mysql_error());
$select = ‘SELECT boardsSold, bootsSold, bindingsSold’;
$from = ‘ FROM boardsrus’;
$queryResult = @mysql_query($select . $from);
if (!$queryResult)
die(‘Error retrieving total boards sold from database.’);
while ($row = mysql_fetch_array($queryResult)) {
$boardsSold = $row[‘boardsSold’];
$bootsSold = $row[‘bootsSold’];
$bindingsSold = $row[‘bindingsSold’];
}
header(“Content-Type: text/xml”);
echo “<?xml version=\”1.0\” encoding=\”utf-8\”?>
?>
<totals>
<boards-sold><? echo $boardsSold; ?></boards-sold>
<boots-sold><? echo $bootsSold; ?></boots-sold>
<bindings-sold><? echo $bindingsSold; ?></bindings-sold>
</totals>
<? mysql_close($conn); ?>
The Boards ‘R’
Us database now
keeps up with three
products: boards,
boots, and bindings.
In this version of the app, we’r ...