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 |
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>"; ?>