demo_db_select_orderby_oo.php:
<!DOCTYPE html> <html> <body> <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 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 FROM MyGuests ORDER BY lastname"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>"; } } else { echo "0 results"; } $conn->close(); ?> </body> </html>
id: 1 - Name: John Doe
id: 3 - Name: Julie Dooley
id: 2 - Name: Mary Moe