Example: Deleting guests using mySQLi Object-oriented

Record deleted successfully

Here is the code for deleting the record from the database table.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "DBname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>

After the record is deleted, the table will look like this:

IDNameEmailRegistrarion Date
1Don Jamesjohn@example.com2024-11-28 13:25:43
2Don Doejohn@example.com2024-11-28 14:34:54
4John Doejohn@deere.net2024-11-28 14:37:15
6Daisy Duckdaisyduck@example.com2024-11-28 13:41:22
7Julie Dooleyjuliedooley@example.com2024-11-28 13:41:22
8Bob Marleybobmarley@example.com2024-11-28 13:43:53
9Rita Marleyritamarley@example.com2024-11-28 13:43:53
10Peter Toshpetertosh@example.com2024-11-28 13:43:53
11Tom Cattc@example.com2024-11-28 13:49:32
12Woody Woodpeckerwoody@example.com2024-11-28 13:49:32
13Mighty Mousemighty@example.com2024-11-28 13:49:32
14Bob Marleybobmarley@example.com2024-12-05 06:36:51
15Rita Marleyritamarley@example.com2024-12-05 06:36:51
16Peter Toshpetertosh@example.com2024-12-05 06:36:51
17Tom Cattc@example.com2024-12-05 06:39:18
18Woody Woodpeckerwoody@example.com2024-12-05 06:39:18
19Mighty Mousemighty@example.com2024-12-05 06:39:18
20John Deerejohn@deere.net2024-12-05 06:39:22
21Don Jamesjohn@example.com2024-12-05 06:41:38
22Donald Duckdduckn@example.com2024-12-05 06:46:06
23Daisy Duckdaisyduck@example.com2024-12-05 06:46:06
24Julie Dooleyjuliedooley@example.com2024-12-05 06:46:06
25John Deerjohnd@example.com2024-12-05 06:57:02
26Don Jamesjohn@example.com2024-12-05 06:57:06
27John Deerjohnd@example.com2024-12-07 02:13:57
28John Deerejohn@deere.net2024-12-07 11:26:18
29John Deerjohnd@example.com2024-12-07 14:43:35
30Don Jamesjohn@example.com2024-12-07 21:24:41
31Donald Duckdduckn@example.com2024-12-07 21:37:50
32Daisy Duckdaisyduck@example.com2024-12-07 21:37:50
33Julie Dooleyjuliedooley@example.com2024-12-07 21:37:50
34Tom Cattc@example.com2024-12-07 22:10:43
35Woody Woodpeckerwoody@example.com2024-12-07 22:10:43
36Mighty Mousemighty@example.com2024-12-07 22:10:43
37Bob Marleybobmarley@example.com2024-12-08 01:08:15
38Rita Marleyritamarley@example.com2024-12-08 01:08:15
39Peter Toshpetertosh@example.com2024-12-08 01:08:15
40Don Jamesjohn@example.com2024-12-08 03:46:03
41Don Jamesjohn@example.com2024-12-13 22:34:50
42John Deerjohnd@example.com2024-12-13 22:39:14
43John Deerejohn@deere.net2024-12-13 22:40:02
44Don Jamesjohn@example.com2024-12-13 22:44:26
45Donald Duckdduckn@example.com2024-12-13 22:44:51
46Daisy Duckdaisyduck@example.com2024-12-13 22:44:51
47Julie Dooleyjuliedooley@example.com2024-12-13 22:44:51
48Tom Cattc@example.com2024-12-13 22:45:14
49Woody Woodpeckerwoody@example.com2024-12-13 22:45:15
50Mighty Mousemighty@example.com2024-12-13 22:45:15
51Bob Marleybobmarley@example.com2024-12-13 22:45:38
52Rita Marleyritamarley@example.com2024-12-13 22:45:38
53Peter Toshpetertosh@example.com2024-12-13 22:45:39
54John Deerjohnd@example.com2024-12-22 19:07:33
55John Deerejohn@deere.net2024-12-22 19:07:42
56Don Jamesjohn@example.com2024-12-22 19:08:12
57Don Jamesjohn@example.com2024-12-22 19:08:19
58Tom Cattc@example.com2024-12-22 19:17:05
59Woody Woodpeckerwoody@example.com2024-12-22 19:17:05
60Mighty Mousemighty@example.com2024-12-22 19:17:05
61Donald Duckdduckn@example.com2024-12-22 19:21:09
62Daisy Duckdaisyduck@example.com2024-12-22 19:21:09
63Julie Dooleyjuliedooley@example.com2024-12-22 19:21:09
64Bob Marleybobmarley@example.com2024-12-22 19:21:17
65Rita Marleyritamarley@example.com2024-12-22 19:21:17
66Peter Toshpetertosh@example.com2024-12-22 19:21:17
67John Deerjohnd@example.com2024-12-28 11:12:45
68John Deerejohn@deere.net2024-12-30 11:12:45
69Bob Marleybobmarley@example.com2024-12-30 15:50:59
70Rita Marleyritamarley@example.com2024-12-30 15:50:59
71Peter Toshpetertosh@example.com2024-12-30 15:50:59
72Donald Duckdduckn@example.com2024-12-30 17:12:49
73Daisy Duckdaisyduck@example.com2024-12-30 17:12:49
74Julie Dooleyjuliedooley@example.com2024-12-30 17:12:49
75John Deerejohn@deere.net2024-12-30 19:31:58
76Tom Cattc@example.com2024-12-30 20:23:19
77Woody Woodpeckerwoody@example.com2024-12-30 20:23:19
78Mighty Mousemighty@example.com2024-12-30 20:23:19
79Don Jamesjohn@example.com2024-12-30 20:26:27
80Bob Marleybobmarley@example.com2024-12-30 20:27:19
81Rita Marleyritamarley@example.com2024-12-30 20:27:19
82Peter Toshpetertosh@example.com2024-12-30 20:27:19
83John Deerjohnd@example.com2024-12-30 20:43:27
84Don Jamesjohn@example.com2024-12-30 20:54:12
85John Deerjohnd@example.com2025-01-03 21:31:26
86John Deerejohn@deere.net2025-01-03 21:37:56
87Don Jamesjohn@example.com2025-01-03 21:39:54
88Don Jamesjohn@example.com2025-01-03 21:42:32
89Tom Cattc@example.com2025-01-03 21:43:14
90Woody Woodpeckerwoody@example.com2025-01-03 21:43:14
91Mighty Mousemighty@example.com2025-01-03 21:43:14
92Donald Duckdduckn@example.com2025-01-03 21:52:13
93Daisy Duckdaisyduck@example.com2025-01-03 21:52:13
94Julie Dooleyjuliedooley@example.com2025-01-03 21:52:13
95Bob Marleybobmarley@example.com2025-01-03 21:53:41
96Rita Marleyritamarley@example.com2025-01-03 21:53:41
97Peter Toshpetertosh@example.com2025-01-03 21:53:41
98Tom Cattc@example.com2025-01-03 23:03:10
99Woody Woodpeckerwoody@example.com2025-01-03 23:03:10
100Mighty Mousemighty@example.com2025-01-03 23:03:10
101Don Jamesjohn@example.com2025-01-03 23:15:10
102Bob Marleybobmarley@example.com2025-01-03 23:16:59
103Rita Marleyritamarley@example.com2025-01-03 23:16:59
104Peter Toshpetertosh@example.com2025-01-03 23:16:59
105John Deerjohnd@example.com2025-01-03 23:28:09
106John Deerejohn@deere.net2025-01-03 23:44:09
107Don Jamesjohn@example.com2025-01-03 23:45:27
108Donald Duckdduckn@example.com2025-01-03 23:47:21
109Daisy Duckdaisyduck@example.com2025-01-03 23:47:21
110Julie Dooleyjuliedooley@example.com2025-01-03 23:47:21
111Bob Marleybobmarley@example.com2025-01-04 14:55:02
112Rita Marleyritamarley@example.com2025-01-04 14:55:02
113Peter Toshpetertosh@example.com2025-01-04 14:55:02
114Tom Cattc@example.com2025-01-04 14:57:32
115Woody Woodpeckerwoody@example.com2025-01-04 14:57:32
116Mighty Mousemighty@example.com2025-01-04 14:57:32
117John Deerejohn@deere.net2025-01-04 14:57:34
118Don Jamesjohn@example.com2025-01-04 14:59:50
119Donald Duckdduckn@example.com2025-01-04 15:04:18
120Daisy Duckdaisyduck@example.com2025-01-04 15:04:18
121Julie Dooleyjuliedooley@example.com2025-01-04 15:04:18
122John Deerjohnd@example.com2025-01-04 15:15:14
123Don Jamesjohn@example.com2025-01-04 15:15:18
124Bob Marleybobmarley@example.com2025-01-04 23:35:42
125Rita Marleyritamarley@example.com2025-01-04 23:35:42
126Peter Toshpetertosh@example.com2025-01-04 23:35:42
127Don Jamesjohn@example.com2025-01-04 23:45:51
128Donald Duckdduckn@example.com2025-01-04 23:50:22
129Daisy Duckdaisyduck@example.com2025-01-04 23:50:22
130Julie Dooleyjuliedooley@example.com2025-01-04 23:50:22
131Don Jamesjohn@example.com2025-01-04 23:53:02
132John Deerejohn@deere.net2025-01-04 23:53:09
133Tom Cattc@example.com2025-01-04 23:53:19
134Woody Woodpeckerwoody@example.com2025-01-04 23:53:19
135Mighty Mousemighty@example.com2025-01-04 23:53:19
136John Deerjohnd@example.com2025-01-07 12:53:10
137Don Jamesjohn@example.com2025-01-07 13:08:59
138Bob Marleybobmarley@example.com2025-01-07 13:34:10
139Rita Marleyritamarley@example.com2025-01-07 13:34:10
140Peter Toshpetertosh@example.com2025-01-07 13:34:10
141John Deerejohn@deere.net2025-01-07 13:42:48
142Tom Cattc@example.com2025-01-07 14:13:15
143Woody Woodpeckerwoody@example.com2025-01-07 14:13:15
144Mighty Mousemighty@example.com2025-01-07 14:13:15
145Donald Duckdduckn@example.com2025-01-07 14:21:01
146Daisy Duckdaisyduck@example.com2025-01-07 14:21:01
147Julie Dooleyjuliedooley@example.com2025-01-07 14:21:01
148Don Jamesjohn@example.com2025-01-07 14:22:09
149Don Jamesjohn@example.com2025-01-09 15:10:06
150Bob Marleybobmarley@example.com2025-01-09 15:10:24
151Rita Marleyritamarley@example.com2025-01-09 15:10:24
152Peter Toshpetertosh@example.com2025-01-09 15:10:24
153John Deerjohnd@example.com2025-01-09 15:21:11
154John Deerejohn@deere.net2025-01-09 15:23:48
155Tom Cattc@example.com2025-01-10 07:29:25
156Woody Woodpeckerwoody@example.com2025-01-10 07:29:25
157Mighty Mousemighty@example.com2025-01-10 07:29:25
158Donald Duckdduckn@example.com2025-01-10 07:43:14
159Daisy Duckdaisyduck@example.com2025-01-10 07:43:14
160Julie Dooleyjuliedooley@example.com2025-01-10 07:43:14
161Don Jamesjohn@example.com2025-01-10 08:09:24
162John Deerjohnd@example.com2025-01-12 01:38:22
163John Deerejohn@deere.net2025-01-12 01:39:24
164Don Jamesjohn@example.com2025-01-12 01:49:26
165Bob Marleybobmarley@example.com2025-01-12 01:50:10
166Rita Marleyritamarley@example.com2025-01-12 01:50:10
167Peter Toshpetertosh@example.com2025-01-12 01:50:10
168Tom Cattc@example.com2025-01-12 11:23:59
169Woody Woodpeckerwoody@example.com2025-01-12 11:23:59
170Mighty Mousemighty@example.com2025-01-12 11:23:59
171Don Jamesjohn@example.com2025-01-12 11:28:26
172Donald Duckdduckn@example.com2025-01-12 11:45:57
173Daisy Duckdaisyduck@example.com2025-01-12 11:45:57
174Julie Dooleyjuliedooley@example.com2025-01-12 11:45:57
175Tom Cattc@example.com2025-01-15 14:30:12
176Woody Woodpeckerwoody@example.com2025-01-15 14:30:12
177Mighty Mousemighty@example.com2025-01-15 14:30:12
178John Deerjohnd@example.com2025-01-15 14:36:01
179John Deerejohn@deere.net2025-01-15 14:37:21
180Donald Duckdduckn@example.com2025-01-15 14:42:22
181Daisy Duckdaisyduck@example.com2025-01-15 14:42:22
182Julie Dooleyjuliedooley@example.com2025-01-15 14:42:22
183Don Jamesjohn@example.com2025-01-15 17:46:24
184Bob Marleybobmarley@example.com2025-01-15 17:47:23
185Rita Marleyritamarley@example.com2025-01-15 17:47:23
186Peter Toshpetertosh@example.com2025-01-15 17:47:23
187Don Jamesjohn@example.com2025-01-15 18:11:17
188Donald Duckdduckn@example.com2025-01-18 04:58:55
189Daisy Duckdaisyduck@example.com2025-01-18 04:58:55
190Julie Dooleyjuliedooley@example.com2025-01-18 04:58:55
191Don Jamesjohn@example.com2025-01-18 05:00:56
192Tom Cattc@example.com2025-01-18 05:04:39
193Woody Woodpeckerwoody@example.com2025-01-18 05:04:39
194Mighty Mousemighty@example.com2025-01-18 05:04:39
195John Deerejohn@deere.net2025-01-18 05:09:13
196Bob Marleybobmarley@example.com2025-01-18 05:10:09
197Rita Marleyritamarley@example.com2025-01-18 05:10:09
198Peter Toshpetertosh@example.com2025-01-18 05:10:09
199Don Jamesjohn@example.com2025-01-18 05:13:07
200John Deerjohnd@example.com2025-01-18 05:14:39

Here is the code for accessing the database and display the information in a table.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "DBname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname, email FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo "<table><tr><th>ID</th><th>Name</th><th>Email</th><Registration Date</th></tr>";
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "<tr style='background-color:yellow';>
<td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td>".$row["email"]."</td><td>".$row["reg_date"]."</td></tr>";
  }
  echo "</table>";
} else {
  echo "<p>0 results</p>";
}
$conn->close();
?>