Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/exercises/mysql-intro_2/
admin.php
createtable.php
dbconnection.php
deletepost.php
deletetable.php
insertdefaultposts.php
insertposts.php
mysql-log-in.php
selectposts.php
updateposts.php
welcome.php
selectposts.php
69 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
<!-- selectposts.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select</title>
</head>
<body>
<?php
include ('dbconnection.php');
/**
* @var PDO $dbconn
* */
try {
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM friend";
$stmt = $dbconn->prepare($sql);
// parameters in array, if empty we could skip the $data-variable
$data = array();
$stmt->execute($data);
$res = $stmt->fetchAll();
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
// fetch width column names
$data = array();
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
// fetch columnindex
$data = array();
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_NUM);
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
// 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['phone_number'])."</td>".
"<td>".htmlentities($res['e_mail'])."</td>".
"<td>".htmlentities($res['admin'])."</td>".
"</tr>";
}
$output .= "</table>";
echo "$output";
}
catch(PDOException $e)
{
echo $sql . "<br />" . $e->getMessage();
}
$dbconn = null;
?>
</body>
</html>