0 records UPDATED successfully
Here is the code for deleting the database record.
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Update the selected record with a new last name $sql = "UPDATE MyGuests SET lastname='' WHERE id=7"; // Prepare statement $stmt = $conn->prepare($sql); // execute the query $stmt->execute(); // echo a message to say the UPDATE succeeded echo "".$stmt->rowCount() . " records UPDATED successfully"; } catch(PDOException $e) { echo "".$sql . "<br>" . $e->getMessage().""; } $conn = null; ?>
This is what the database table looks like after the record is deleted.
Id | Firstname | Lastname | Registration Date | |
---|---|---|---|---|
1 | John | Doe | john@example.com | 2024-11-28 13:31:22 |
2 | John | Deere | john@deere.net | 2024-11-28 13:38:46 |
4 | Daisy | Duck | daisyduck@example.com | 2024-11-28 13:46:18 |
5 | Pluto | pluto@example.com | 2024-11-28 13:46:18 | |
6 | Donald | Duck | dduck@example.com | 2024-12-05 06:47:46 |
7 | Daisy | daisyduck@example.com | 2024-12-05 06:48:54 | |
8 | Pluto | pluto@example.com | 2024-12-05 06:47:46 | |
9 | John | Deere | john@deere.net | 2024-12-05 06:54:14 |
10 | John | Doe | john@example.com | 2024-12-05 06:54:18 |
11 | Donald | Duck | dduck@example.com | 2024-12-07 02:11:50 |
12 | Daisy | Duck | daisyduck@example.com | 2024-12-07 02:11:50 |
13 | Pluto | pluto@example.com | 2024-12-07 02:11:50 | |
14 | John | Deere | john@deere.net | 2024-12-07 02:13:49 |
15 | John | Doe | john@example.com | 2024-12-08 00:09:04 |
16 | John | Deere | john@deere.net | 2024-12-08 03:13:10 |
17 | Donald | Duck | dduck@example.com | 2024-12-08 03:39:28 |
18 | Daisy | Duck | daisyduck@example.com | 2024-12-08 03:39:28 |
19 | Pluto | pluto@example.com | 2024-12-08 03:39:28 | |
20 | Donald | Duck | dduck@example.com | 2024-12-13 22:35:38 |
21 | Daisy | Duck | daisyduck@example.com | 2024-12-13 22:35:38 |
22 | Pluto | pluto@example.com | 2024-12-13 22:35:38 | |
23 | John | Doe | john@example.com | 2024-12-13 22:37:14 |
24 | John | Deere | john@deere.net | 2024-12-13 22:42:26 |
25 | John | Deere | john@deere.net | 2024-12-20 01:25:39 |
26 | John | Deere | john@deere.net | 2024-12-22 19:07:39 |
27 | John | Doe | john@example.com | 2024-12-22 19:08:16 |
28 | Donald | Duck | dduck@example.com | 2024-12-22 19:21:13 |
29 | Daisy | Duck | daisyduck@example.com | 2024-12-22 19:21:13 |
30 | Pluto | pluto@example.com | 2024-12-22 19:21:13 | |
31 | John | Deere | john@deere.net | 2024-12-28 09:01:18 |
32 | John | Doe | john@example.com | 2024-12-30 17:05:24 |
33 | Donald | Duck | dduck@example.com | 2024-12-30 17:07:26 |
34 | Daisy | Duck | daisyduck@example.com | 2024-12-30 17:07:26 |
35 | Pluto | pluto@example.com | 2024-12-30 17:07:26 | |
36 | John | Deere | john@deere.net | 2025-01-03 21:34:45 |
37 | John | Doe | john@example.com | 2025-01-03 21:41:36 |
38 | Donald | Duck | dduck@example.com | 2025-01-03 21:53:00 |
39 | Daisy | Duck | daisyduck@example.com | 2025-01-03 21:53:00 |
40 | Pluto | pluto@example.com | 2025-01-03 21:53:00 | |
41 | John | Doe | john@example.com | 2025-01-03 23:02:47 |
42 | Donald | Duck | dduck@example.com | 2025-01-03 23:08:58 |
43 | Daisy | Duck | daisyduck@example.com | 2025-01-03 23:08:58 |
44 | Pluto | pluto@example.com | 2025-01-03 23:08:58 | |
45 | Donald | Duck | dduck@example.com | 2025-01-04 15:05:58 |
46 | Daisy | Duck | daisyduck@example.com | 2025-01-04 15:05:58 |
47 | Pluto | pluto@example.com | 2025-01-04 15:05:58 | |
48 | John | Deere | john@deere.net | 2025-01-04 15:12:26 |
49 | John | Doe | john@example.com | 2025-01-04 15:12:30 |
50 | John | Doe | john@example.com | 2025-01-04 23:46:11 |
51 | Donald | Duck | dduck@example.com | 2025-01-07 14:30:59 |
52 | Daisy | Duck | daisyduck@example.com | 2025-01-07 14:30:59 |
53 | Pluto | pluto@example.com | 2025-01-07 14:30:59 | |
54 | John | Doe | john@example.com | 2025-01-07 14:50:22 |
55 | John | Deere | john@deere.net | 2025-01-07 18:49:25 |
56 | Donald | Duck | dduck@example.com | 2025-01-10 07:32:28 |
57 | Daisy | Duck | daisyduck@example.com | 2025-01-10 07:32:28 |
58 | Pluto | pluto@example.com | 2025-01-10 07:32:28 | |
59 | John | Doe | john@example.com | 2025-01-10 08:29:05 |
60 | John | Deere | john@deere.net | 2025-01-11 09:59:18 |
61 | Donald | Duck | dduck@example.com | 2025-01-12 11:28:23 |
62 | Daisy | Duck | daisyduck@example.com | 2025-01-12 11:28:23 |
63 | Pluto | pluto@example.com | 2025-01-12 11:28:23 | |
64 | John | Doe | john@example.com | 2025-01-12 14:18:21 |
65 | John | Deere | john@deere.net | 2025-01-14 05:19:18 |
66 | Donald | Duck | dduck@example.com | 2025-01-15 13:03:58 |
67 | Daisy | Duck | daisyduck@example.com | 2025-01-15 13:03:58 |
68 | Pluto | pluto@example.com | 2025-01-15 13:03:58 | |
69 | John | Deere | john@deere.net | 2025-01-18 05:12:59 |
70 | John | Doe | john@example.com | 2025-01-18 05:15:35 |
71 | Donald | Duck | dduck@example.com | 2025-01-18 05:16:49 |
72 | Daisy | Duck | daisyduck@example.com | 2025-01-18 05:16:49 |
73 | Pluto | pluto@example.com | 2025-01-18 05:16:49 | |
74 | Donald | Duck | dduck@example.com | 2025-01-21 14:48:48 |
75 | Daisy | Duck | daisyduck@example.com | 2025-01-21 14:48:48 |
76 | Pluto | pluto@example.com | 2025-01-21 14:48:48 | |
77 | John | Doe | john@example.com | 2025-01-21 14:54:58 |
78 | John | Deere | john@deere.net | 2025-01-21 15:34:18 |
79 | John | Deere | john@deere.net | 2025-01-25 18:40:04 |
80 | Donald | Duck | dduck@example.com | 2025-01-25 18:40:07 |
81 | Daisy | Duck | daisyduck@example.com | 2025-01-25 18:40:07 |
82 | Pluto | pluto@example.com | 2025-01-25 18:40:07 | |
83 | Donald | Duck | dduck@example.com | 2025-01-29 15:00:25 |
84 | Daisy | Duck | daisyduck@example.com | 2025-01-29 15:00:25 |
85 | Pluto | pluto@example.com | 2025-01-29 15:00:25 | |
86 | John | Doe | john@example.com | 2025-01-29 15:01:13 |
87 | John | Deere | john@deere.net | 2025-02-02 00:40:07 |
88 | John | Doe | john@example.com | 2025-02-02 00:54:03 |
89 | Donald | Duck | dduck@example.com | 2025-02-02 02:05:36 |
90 | Daisy | Duck | daisyduck@example.com | 2025-02-02 02:05:36 |
91 | Pluto | pluto@example.com | 2025-02-02 02:05:36 | |
92 | Donald | Duck | dduck@example.com | 2025-02-03 16:49:11 |
93 | Daisy | Duck | daisyduck@example.com | 2025-02-03 16:49:11 |
94 | Pluto | pluto@example.com | 2025-02-03 16:49:11 | |
95 | John | Deere | john@deere.net | 2025-02-03 16:56:12 |
96 | John | Doe | john@example.com | 2025-02-03 16:56:16 |
97 | John | Doe | john@example.com | 2025-02-05 21:31:33 |
98 | John | Deere | john@deere.net | 2025-02-07 13:13:13 |
99 | John | Doe | john@example.com | 2025-02-07 13:14:21 |
100 | Donald | Duck | dduck@example.com | 2025-02-07 13:15:55 |
101 | Daisy | Duck | daisyduck@example.com | 2025-02-07 13:15:55 |
102 | Pluto | pluto@example.com | 2025-02-07 13:15:55 | |
103 | John | Deere | john@deere.net | 2025-02-12 06:33:17 |
104 | John | Doe | john@example.com | 2025-02-12 06:34:12 |
105 | Donald | Duck | dduck@example.com | 2025-02-12 06:35:54 |
106 | Daisy | Duck | daisyduck@example.com | 2025-02-12 06:35:54 |
107 | Pluto | pluto@example.com | 2025-02-12 06:35:54 | |
108 | John | Deere | john@deere.net | 2025-02-21 13:02:39 |
109 | Donald | Duck | dduck@example.com | 2025-02-21 13:12:36 |
110 | Daisy | Duck | daisyduck@example.com | 2025-02-21 13:12:36 |
111 | Pluto | pluto@example.com | 2025-02-21 13:12:36 | |
112 | John | Doe | john@example.com | 2025-02-21 13:26:03 |
113 | John | Deere | john@deere.net | 2025-02-23 01:44:24 |
114 | Donald | Duck | dduck@example.com | 2025-02-23 02:09:24 |
115 | Daisy | Duck | daisyduck@example.com | 2025-02-23 02:09:24 |
116 | Pluto | pluto@example.com | 2025-02-23 02:09:24 | |
117 | John | Doe | john@example.com | 2025-02-23 02:14:24 |
118 | John | Deere | john@deere.net | 2025-02-23 04:59:24 |
119 | John | Deere | john@deere.net | 2025-02-24 11:11:25 |
120 | John | Doe | john@example.com | 2025-02-24 11:24:18 |
121 | Donald | Duck | dduck@example.com | 2025-02-24 11:39:01 |
122 | Daisy | Duck | daisyduck@example.com | 2025-02-24 11:39:01 |
123 | Pluto | pluto@example.com | 2025-02-24 11:39:01 | |
124 | John | Deere | john@deere.net | 2025-02-26 22:11:22 |
125 | Donald | Duck | dduck@example.com | 2025-02-26 22:58:33 |
126 | Daisy | Duck | daisyduck@example.com | 2025-02-26 22:58:33 |
127 | Pluto | pluto@example.com | 2025-02-26 22:58:33 | |
128 | John | Doe | john@example.com | 2025-03-06 01:19:41 |
129 | Donald | Duck | dduck@example.com | 2025-03-08 14:04:03 |
130 | Daisy | Duck | daisyduck@example.com | 2025-03-08 14:04:03 |
131 | Pluto | pluto@example.com | 2025-03-08 14:04:03 | |
132 | John | Deere | john@deere.net | 2025-03-08 14:08:49 |
133 | John | Doe | john@example.com | 2025-03-08 14:08:53 |
134 | John | Deere | john@deere.net | 2025-03-11 15:32:13 |
135 | Donald | Duck | dduck@example.com | 2025-03-16 11:34:08 |
136 | Daisy | Duck | daisyduck@example.com | 2025-03-16 11:34:08 |
137 | Pluto | pluto@example.com | 2025-03-16 11:34:08 | |
138 | John | Doe | john@example.com | 2025-03-18 00:19:51 |
139 | Donald | Duck | dduck@example.com | 2025-03-18 00:21:36 |
140 | Daisy | Duck | daisyduck@example.com | 2025-03-18 00:21:36 |
141 | Pluto | pluto@example.com | 2025-03-18 00:21:36 | |
142 | Donald | Duck | dduck@example.com | 2025-03-20 01:47:24 |
143 | Daisy | Duck | daisyduck@example.com | 2025-03-20 01:47:24 |
144 | Pluto | pluto@example.com | 2025-03-20 01:47:24 | |
145 | John | Deere | john@deere.net | 2025-03-20 01:47:25 |
146 | John | Doe | john@example.com | 2025-03-20 01:47:25 |
147 | Donald | Duck | dduck@example.com | 2025-03-22 11:26:09 |
148 | Daisy | Duck | daisyduck@example.com | 2025-03-22 11:26:09 |
149 | Pluto | pluto@example.com | 2025-03-22 11:26:09 | |
150 | John | Doe | john@example.com | 2025-03-24 22:18:12 |
151 | John | Doe | john@example.com | 2025-03-28 09:06:47 |
152 | John | Deere | john@deere.net | 2025-03-31 09:05:19 |
153 | John | Deere | john@deere.net | 2025-04-02 21:44:03 |
154 | John | Doe | john@example.com | 2025-04-02 21:51:52 |
155 | Donald | Duck | dduck@example.com | 2025-04-02 23:01:36 |
156 | Daisy | Duck | daisyduck@example.com | 2025-04-02 23:01:36 |
157 | Pluto | pluto@example.com | 2025-04-02 23:01:36 | |
158 | John | Doe | john@example.com | 2025-04-03 03:42:03 |
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><th>Registration Date</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, reg_date 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>"; ?>