| Id | Firstname | Lastname |
|---|
| 1 | John | Doe |
| 2 | John | Clot |
| 4 | John | Doe |
| 5 | John | Doe |
| 6 | Mary | Moe |
| 7 | Julie | Dooley |
| 8 | John | Doe |
| 9 | Mary | Moe |
| 10 | Julie | Dooley |
| 11 | John | Doe |
| 12 | Mary | Moe |
| 13 | Julie | Dooley |
| 14 | John | Doe |
| 15 | John | Doe |
| 16 | John | Doe |
| 17 | Mary | Moe |
| 18 | Julie | Dooley |
| 19 | John | Doe |
| 20 | Mary | Moe |
| 21 | Julie | Dooley |
| 22 | John | Doe |
| 23 | John | Doe |
| 24 | John | Doe |
| 25 | John | Doe |
| 26 | Mary | Moe |
| 27 | Julie | Dooley |
| 28 | John | Doe |
| 29 | Mary | Moe |
| 30 | Julie | Dooley |
| 31 | John | Doe |
| 32 | John | Doe |
| 33 | John | Doe |
| 34 | John | Doe |
| 35 | John | Doe |
| 36 | John | Doe |
| 37 | John | Doe |
| 38 | John | Doe |
| 39 | Mary | Moe |
| 40 | Julie | Dooley |
| 41 | John | Doe |
| 42 | John | Doe |
| 43 | Mary | Moe |
| 44 | Julie | Dooley |
| 45 | John | Doe |
| 46 | John | Doe |
| 47 | Mary | Moe |
| 48 | Julie | Dooley |
| 49 | John | Doe |
| 50 | John | Doe |
| 51 | Mary | Moe |
| 52 | Julie | Dooley |
| 53 | John | Doe |
| 54 | John | Doe |
| 55 | Mary | Moe |
| 56 | Julie | Dooley |
| 57 | John | Doe |
| 58 | John | Doe |
| 59 | Mary | Moe |
| 60 | Julie | Dooley |
| 61 | John | Doe |
| 62 | John | Doe |
| 63 | Mary | Moe |
| 64 | Julie | Dooley |
| 65 | John | Doe |
| 66 | Mary | Moe |
| 67 | Julie | Dooley |
| 68 | John | Doe |
| 69 | John | Doe |
| 70 | John | Doe |
| 71 | Mary | Moe |
| 72 | Julie | Dooley |
| 73 | John | Doe |
| 74 | Mary | Moe |
| 75 | Julie | Dooley |
| 76 | John | Doe |
| 77 | John | Doe |
| 78 | Mary | Moe |
| 79 | Julie | Dooley |
| 80 | John | Doe |
| 81 | Mary | Moe |
| 82 | Julie | Dooley |
| 83 | John | Doe |
| 84 | Mary | Moe |
| 85 | Julie | Dooley |
| 86 | John | Doe |
| 87 | John | Doe |
| 88 | John | Doe |
| 89 | Mary | Moe |
| 90 | Julie | Dooley |
| 91 | John | Doe |
| 92 | John | Doe |
| 93 | Mary | Moe |
| 94 | Julie | Dooley |
| 95 | John | Doe |
| 96 | John | Doe |
| 97 | Mary | Moe |
| 98 | Julie | Dooley |
| 99 | John | Doe |
| 100 | Mary | Moe |
| 101 | Julie | Dooley |
| 102 | John | Doe |
| 103 | John | Doe |
| 104 | John | Doe |
| 105 | Mary | Moe |
| 106 | Julie | Dooley |
| 107 | John | Doe |
| 108 | John | Doe |
| 109 | Mary | Moe |
| 110 | Julie | Dooley |
| 111 | John | Doe |
| 112 | Mary | Moe |
| 113 | Julie | Dooley |
| 114 | John | Doe |
| 115 | John | Doe |
| 116 | John | Doe |
| 117 | Mary | Moe |
| 118 | Julie | Dooley |
| 119 | John | Doe |
| 120 | John | Doe |
| 121 | Mary | Moe |
| 122 | Julie | Dooley |
| 123 | John | Doe |
| 124 | John | Doe |
| 125 | Mary | Moe |
| 126 | Julie | Dooley |
Codi 07_seleccioDeDades.php:
<!doctype html>
<HTML lang="ca">
<HEAD>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<TITLE>Exemple de selecció de dades</TITLE>
</HEAD>
<BODY>
<?php
$prg = "07_seleccioDeDades.php";
// Example (PDO) at https://www.w3schools.com/php/php_mysql_select.asp
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current(): string { // Originalment function current() { // Afegit el tipus que retorna per evitar missatge
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren(): void { // Afegit el tipus que retorna per evitar missatge Deprecated
echo "<tr>";
}
function endChildren(): void { // Afegit el tipus que retorna per evitar missatge Deprecated
echo "</tr>" . "\n";
}
}
$servername = "172.18.0.12"; // docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' sm9b_binefa_cat_db_docker
$username = "root"; // Poseu un usuari de la BD en comptes de root
$password = "fjeclot";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
<BR>
<h1>Codi <?php echo $prg;?>: </h1>
<?php
show_source($prg);
?>
</BODY>
</HTML>