Record deleted 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); // sql to delete a record $sql = "DELETE FROM MyGuests WHERE id=3"; // use exec() because no results are returned $conn->exec($sql); echo "Record deleted 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 | |
---|---|---|---|
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 | |
74 | Donald | Duck | dduck@example.com |
75 | Daisy | Duck | daisyduck@example.com |
76 | Pluto | pluto@example.com | |
77 | John | Doe | john@example.com |
78 | John | Deere | john@deere.net |
79 | John | Deere | john@deere.net |
80 | Donald | Duck | dduck@example.com |
81 | Daisy | Duck | daisyduck@example.com |
82 | Pluto | pluto@example.com | |
83 | Donald | Duck | dduck@example.com |
84 | Daisy | Duck | daisyduck@example.com |
85 | Pluto | pluto@example.com | |
86 | John | Doe | john@example.com |
87 | John | Deere | john@deere.net |
88 | John | Doe | john@example.com |
89 | Donald | Duck | dduck@example.com |
90 | Daisy | Duck | daisyduck@example.com |
91 | Pluto | pluto@example.com | |
92 | Donald | Duck | dduck@example.com |
93 | Daisy | Duck | daisyduck@example.com |
94 | Pluto | pluto@example.com | |
95 | John | Deere | john@deere.net |
96 | John | Doe | john@example.com |
97 | John | Doe | john@example.com |
98 | John | Deere | john@deere.net |
99 | John | Doe | john@example.com |
100 | Donald | Duck | dduck@example.com |
101 | Daisy | Duck | daisyduck@example.com |
102 | Pluto | pluto@example.com | |
103 | John | Deere | john@deere.net |
104 | John | Doe | john@example.com |
105 | Donald | Duck | dduck@example.com |
106 | Daisy | Duck | daisyduck@example.com |
107 | Pluto | pluto@example.com | |
108 | John | Deere | john@deere.net |
109 | Donald | Duck | dduck@example.com |
110 | Daisy | Duck | daisyduck@example.com |
111 | Pluto | pluto@example.com | |
112 | John | Doe | john@example.com |
113 | John | Deere | john@deere.net |
114 | Donald | Duck | dduck@example.com |
115 | Daisy | Duck | daisyduck@example.com |
116 | Pluto | pluto@example.com | |
117 | John | Doe | john@example.com |
118 | John | Deere | john@deere.net |
119 | John | Deere | john@deere.net |
120 | John | Doe | john@example.com |
121 | Donald | Duck | dduck@example.com |
122 | Daisy | Duck | daisyduck@example.com |
123 | Pluto | pluto@example.com | |
124 | John | Deere | john@deere.net |
125 | Donald | Duck | dduck@example.com |
126 | Daisy | Duck | daisyduck@example.com |
127 | Pluto | pluto@example.com | |
128 | John | Doe | john@example.com |
129 | Donald | Duck | dduck@example.com |
130 | Daisy | Duck | daisyduck@example.com |
131 | Pluto | pluto@example.com | |
132 | John | Deere | john@deere.net |
133 | John | Doe | john@example.com |
134 | John | Deere | john@deere.net |
135 | Donald | Duck | dduck@example.com |
136 | Daisy | Duck | daisyduck@example.com |
137 | Pluto | pluto@example.com | |
138 | John | Doe | john@example.com |
139 | Donald | Duck | dduck@example.com |
140 | Daisy | Duck | daisyduck@example.com |
141 | Pluto | pluto@example.com | |
142 | Donald | Duck | dduck@example.com |
143 | Daisy | Duck | daisyduck@example.com |
144 | Pluto | pluto@example.com | |
145 | John | Deere | john@deere.net |
146 | John | Doe | john@example.com |
147 | Donald | Duck | dduck@example.com |
148 | Daisy | Duck | daisyduck@example.com |
149 | Pluto | pluto@example.com | |
150 | John | Doe | john@example.com |
151 | John | Doe | john@example.com |
152 | John | Deere | john@deere.net |
153 | John | Deere | john@deere.net |
154 | John | Doe | john@example.com |
155 | Donald | Duck | dduck@example.com |
156 | Daisy | Duck | daisyduck@example.com |
157 | Pluto | pluto@example.com | |
158 | John | Doe | john@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>"; ?>