Delete guests examples (PDO)

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.

IdFirstnameLastnameEmail
1JohnDoejohn@example.com
2JohnDeerejohn@deere.net
4DaisyDuckdaisyduck@example.com
5Plutopluto@example.com
6DonaldDuckdduck@example.com
7Daisydaisyduck@example.com
8Plutopluto@example.com
9JohnDeerejohn@deere.net
10JohnDoejohn@example.com
11DonaldDuckdduck@example.com
12DaisyDuckdaisyduck@example.com
13Plutopluto@example.com
14JohnDeerejohn@deere.net
15JohnDoejohn@example.com
16JohnDeerejohn@deere.net
17DonaldDuckdduck@example.com
18DaisyDuckdaisyduck@example.com
19Plutopluto@example.com
20DonaldDuckdduck@example.com
21DaisyDuckdaisyduck@example.com
22Plutopluto@example.com
23JohnDoejohn@example.com
24JohnDeerejohn@deere.net
25JohnDeerejohn@deere.net
26JohnDeerejohn@deere.net
27JohnDoejohn@example.com
28DonaldDuckdduck@example.com
29DaisyDuckdaisyduck@example.com
30Plutopluto@example.com
31JohnDeerejohn@deere.net
32JohnDoejohn@example.com
33DonaldDuckdduck@example.com
34DaisyDuckdaisyduck@example.com
35Plutopluto@example.com
36JohnDeerejohn@deere.net
37JohnDoejohn@example.com
38DonaldDuckdduck@example.com
39DaisyDuckdaisyduck@example.com
40Plutopluto@example.com
41JohnDoejohn@example.com
42DonaldDuckdduck@example.com
43DaisyDuckdaisyduck@example.com
44Plutopluto@example.com
45DonaldDuckdduck@example.com
46DaisyDuckdaisyduck@example.com
47Plutopluto@example.com
48JohnDeerejohn@deere.net
49JohnDoejohn@example.com
50JohnDoejohn@example.com
51DonaldDuckdduck@example.com
52DaisyDuckdaisyduck@example.com
53Plutopluto@example.com
54JohnDoejohn@example.com
55JohnDeerejohn@deere.net
56DonaldDuckdduck@example.com
57DaisyDuckdaisyduck@example.com
58Plutopluto@example.com
59JohnDoejohn@example.com
60JohnDeerejohn@deere.net
61DonaldDuckdduck@example.com
62DaisyDuckdaisyduck@example.com
63Plutopluto@example.com
64JohnDoejohn@example.com
65JohnDeerejohn@deere.net
66DonaldDuckdduck@example.com
67DaisyDuckdaisyduck@example.com
68Plutopluto@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>";
?>