Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/exercises/dbintrotest/
10 filer
create_table.php
database_handler.php
dbconnection.php
dbintrotest.php
delete_row.php
delete_table.php
insert_default_values.php
insert_values.php
print_table.php
update_values.php
database_handler.php
dbconnection.php
dbintrotest.php
delete_row.php
delete_table.php
insert_default_values.php
insert_values.php
print_table.php
update_values.php
print_table.php
75 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Print table</title>
<meta charset="utf-8">
</head>
<body>
<?php
include ('dbconnection.php');
try {
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM pdodemotable";
$stmt = $dbconn->prepare($sql);
// parameters in array, if empty we could skip the $data-variable
// ALREADY DONE FOR EACH METHOD (except method 1?)
// $data = array();
// $stmt->execute($data);
// METHOD 1
/*
$res = $stmt->fetchAll();
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
*/
// METHOD 2
// fetch width column names (fetch "using" associative indexes/string indexes)
/*
$data = array();
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
*/
// METHOD 3
// fetch columnindex (fetch "using" numerical indexes)
/*
$data = array();
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_NUM);
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
*/
// METHOD 4 - best!?
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output = "<table><caption>En ostylad tabell!</caption>";
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$output .= "<tr>".
"<td>".htmlentities($res['id'])."</td>".
"<td>".htmlentities($res['firstname'])."</td>".
"<td>".htmlentities($res['lastname'])."</td>".
"<td>".htmlentities($res['age'])."</td>".
"<td>".htmlentities($res['reg_date'])."</td>".
"</tr>";
}
$output .= "</table>";
echo "$output";
}
catch(PDOException $e)
{
echo $sql . "<br />" . $e->getMessage();
}
$dbconn = null;
?>
</body>
</html>