Here's the code:
- Code: Select all
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<body>
<?php
require_once "../../configdata/config.inc.php";
require_once "HTML/Table.php";
dbDemoPDO();
function dbDemoPDO($dbms="mysql") {
try
{
$config = new Config();
$limit = 50;
$dbh = new PDO($config->buildPDODSN($dbms), $config->getUser(), $config->getPassword());
echo("<h2>PDO Database API ($dbms DB)</h2>");
$attributes = array("width"=>"80%", "border"=>"1");
$table = new HTML_Table($attributes);
$attributes = array("class"=>"chart");
$table->addRow(array("City", "Population", "Country", "State"), $attributes, "TH");
$query = "
SELECT city.Name, city.Population, country.Name, city.District
FROM city
INNER JOIN country ON city.CountryCode = country.Code
ORDER BY city.Population DESC LIMIT $limit
";
echo "<p>query is $query</p>";
/* Execute a selection query. */
$cursor = NULL;
// Uncommenting the following line causes Apache to crash...
//$cursor = $dbh->query($query);
foreach ($cursor as $row) {
$name = $row[0];
$population = $row[1];
$country = $row[2];
$state = $row[3];
$contents = array($name,
number_format($population),
$country,
$state);
$table->addRow($contents, $attributes);
}
$table->setColAttributes(1,array("bgcolor"=>"#76A4D1","class"=>"chartnumber"));
$table->setRowAttributes(0,array("bgcolor"=>"#FFFF84","class"=>"chart"));
$attributes1 = "bgcolor = #76A4D1";
$attributes2 = "bgcolor = #B6DCD5";
$table->altRowAttributes(1, $attributes1, $attributes2);
echo $table->toHTML();
/* Disconnect from the database. */
$dbh = NULL;
}
catch (Exception $e)
{
echo("<p>Exception occurred: " . $e->getMessage() . "</p>");
}
}
?>
</body>
</html>