Commit 97c6d7f6 authored by thibaut-felten's avatar thibaut-felten

TP PDO

parent 7d6d2e97
<?php
define('_MYSQL_HOST','127.0.0.1');
define('_MYSQL_PORT',3306);
define('_MYSQL_DBNAME','dbtest');
define('_MYSQL_USER','root');
define('_MYSQL_PASSWORD','root');
$connectionString = "mysql:host=". _MYSQL_HOST;
if(defined('_MYSQL_PORT'))
$connectionString .= ";port=". _MYSQL_PORT;
$connectionString .= ";dbname=" . _MYSQL_DBNAME;
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' );
try {
$pdo = new PDO($connectionString,_MYSQL_USER,_MYSQL_PASSWORD,$options);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $erreur) {
myLog('Erreur : '.$erreur->getMessage());
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TP1</title>
</head>
<body>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
// initialise une variable $pdo connecté à la base locale
require_once("initPDO.php"); // cf. doc / cours
$request = $pdo->prepare("select * from users");
// à vous de compléter...
// afficher un tableau HTML avec les donnéees en utilisant fetch(PDO::FETCH_OBJ)
/*** close the database connection ***/
$request->execute();
while($result = $request->fetch()){
echo("<tr>");
echo("<td>". $result["id"]. "</td>");
echo("<td>". $result["name"]. "</td>");
echo("<td>". $result["email"]. "</td>");
echo("</tr>");
}
echo("</table");
?>
</tbody>
</table>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment