Id | Firstname | Lastname | |
---|---|---|---|
1 | John | Doe | john@example.com |
2 | John | Deere | john@deere.net |
4 | Daisy | Duck | daisyduck@example.com |
5 | Pluto | pluto@example.com | |
6 | Donald | Duck | dduck@example.com |
7 | Daisy | daisyduck@example.com | |
8 | Pluto | pluto@example.com | |
9 | John | Deere | john@deere.net |
10 | John | Doe | john@example.com |
11 | Donald | Duck | dduck@example.com |
12 | Daisy | Duck | daisyduck@example.com |
13 | Pluto | pluto@example.com | |
14 | John | Deere | john@deere.net |
15 | John | Doe | john@example.com |
16 | John | Deere | john@deere.net |
17 | Donald | Duck | dduck@example.com |
18 | Daisy | Duck | daisyduck@example.com |
19 | Pluto | pluto@example.com | |
20 | Donald | Duck | dduck@example.com |
21 | Daisy | Duck | daisyduck@example.com |
22 | Pluto | pluto@example.com | |
23 | John | Doe | john@example.com |
24 | John | Deere | john@deere.net |
25 | John | Deere | john@deere.net |
26 | John | Deere | john@deere.net |
27 | John | Doe | john@example.com |
28 | Donald | Duck | dduck@example.com |
29 | Daisy | Duck | daisyduck@example.com |
30 | Pluto | pluto@example.com | |
31 | John | Deere | john@deere.net |
32 | John | Doe | john@example.com |
33 | Donald | Duck | dduck@example.com |
34 | Daisy | Duck | daisyduck@example.com |
35 | Pluto | pluto@example.com | |
36 | John | Deere | john@deere.net |
37 | John | Doe | john@example.com |
38 | Donald | Duck | dduck@example.com |
39 | Daisy | Duck | daisyduck@example.com |
40 | Pluto | pluto@example.com | |
41 | John | Doe | john@example.com |
42 | Donald | Duck | dduck@example.com |
43 | Daisy | Duck | daisyduck@example.com |
44 | Pluto | pluto@example.com | |
45 | Donald | Duck | dduck@example.com |
46 | Daisy | Duck | daisyduck@example.com |
47 | Pluto | pluto@example.com | |
48 | John | Deere | john@deere.net |
49 | John | Doe | john@example.com |
50 | John | Doe | john@example.com |
51 | Donald | Duck | dduck@example.com |
52 | Daisy | Duck | daisyduck@example.com |
53 | Pluto | pluto@example.com | |
54 | John | Doe | john@example.com |
55 | John | Deere | john@deere.net |
56 | Donald | Duck | dduck@example.com |
57 | Daisy | Duck | daisyduck@example.com |
58 | Pluto | pluto@example.com | |
59 | John | Doe | john@example.com |
60 | John | Deere | john@deere.net |
61 | Donald | Duck | dduck@example.com |
62 | Daisy | Duck | daisyduck@example.com |
63 | Pluto | pluto@example.com | |
64 | John | Doe | john@example.com |
65 | John | Deere | john@deere.net |
66 | Donald | Duck | dduck@example.com |
67 | Daisy | Duck | daisyduck@example.com |
68 | Pluto | pluto@example.com | |
69 | John | Deere | john@deere.net |
70 | John | Doe | john@example.com |
71 | Donald | Duck | dduck@example.com |
72 | Daisy | Duck | daisyduck@example.com |
73 | Pluto | pluto@example.com |
Here is the code for accessing the database and display the information.
<?php echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th><th>Email</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr style='width:150px;border:1px solid black;background-color:yellow;'>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Define and execute prepared statement $stmt = $conn->prepare("SELECT id, firstname, lastname, email 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>"; ?>