Example: Order-by using PDO and prepared statement

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

Here is the code for accessing the database and display the information.

<?php
// Define table for displaying the data
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;background-color:yellow;'>" . 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 = "DBname";

// Create connection
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 WHERE lastname='Doe'");
  $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>";
?>