Examples: Filtering guests with "Where" using mySQLi Object-oriented

id: 2 - Name: Don Doe
id: 4 - Name: John Doe

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

<?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 FROM MyGuests WHERE lastname='Doe'";
$result = $conn->query($sql);

    echo "<p><mark>";
if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  }
} else {
    echo "0 results";
}
    echo "</mark></p>";

$conn->close();
?>

Simular example displayed in a table using "LIKE" and the "%" wildcard

IDNameEmail
2Don Doejohn@example.com
4John Doejohn@deere.net
6Daisy Duckdaisyduck@example.com
7Julie Dooleyjuliedooley@example.com
20John Deerejohn@deere.net
22Donald Duckdduckn@example.com
23Daisy Duckdaisyduck@example.com
24Julie Dooleyjuliedooley@example.com
25John Deerjohnd@example.com
27John Deerjohnd@example.com
28John Deerejohn@deere.net
29John Deerjohnd@example.com
31Donald Duckdduckn@example.com
32Daisy Duckdaisyduck@example.com
33Julie Dooleyjuliedooley@example.com
42John Deerjohnd@example.com
43John Deerejohn@deere.net
45Donald Duckdduckn@example.com
46Daisy Duckdaisyduck@example.com
47Julie Dooleyjuliedooley@example.com
54John Deerjohnd@example.com
55John Deerejohn@deere.net
61Donald Duckdduckn@example.com
62Daisy Duckdaisyduck@example.com
63Julie Dooleyjuliedooley@example.com
67John Deerjohnd@example.com
68John Deerejohn@deere.net
72Donald Duckdduckn@example.com
73Daisy Duckdaisyduck@example.com
74Julie Dooleyjuliedooley@example.com
75John Deerejohn@deere.net
83John Deerjohnd@example.com
85John Deerjohnd@example.com
86John Deerejohn@deere.net
92Donald Duckdduckn@example.com
93Daisy Duckdaisyduck@example.com
94Julie Dooleyjuliedooley@example.com
105John Deerjohnd@example.com
106John Deerejohn@deere.net
108Donald Duckdduckn@example.com
109Daisy Duckdaisyduck@example.com
110Julie Dooleyjuliedooley@example.com
117John Deerejohn@deere.net
119Donald Duckdduckn@example.com
120Daisy Duckdaisyduck@example.com
121Julie Dooleyjuliedooley@example.com
122John Deerjohnd@example.com
128Donald Duckdduckn@example.com
129Daisy Duckdaisyduck@example.com
130Julie Dooleyjuliedooley@example.com
132John Deerejohn@deere.net
136John Deerjohnd@example.com
141John Deerejohn@deere.net
145Donald Duckdduckn@example.com
146Daisy Duckdaisyduck@example.com
147Julie Dooleyjuliedooley@example.com
153John Deerjohnd@example.com
154John Deerejohn@deere.net
158Donald Duckdduckn@example.com
159Daisy Duckdaisyduck@example.com
160Julie Dooleyjuliedooley@example.com
162John Deerjohnd@example.com
163John Deerejohn@deere.net
172Donald Duckdduckn@example.com
173Daisy Duckdaisyduck@example.com
174Julie Dooleyjuliedooley@example.com
178John Deerjohnd@example.com
179John Deerejohn@deere.net
180Donald Duckdduckn@example.com
181Daisy Duckdaisyduck@example.com
182Julie Dooleyjuliedooley@example.com
188Donald Duckdduckn@example.com
189Daisy Duckdaisyduck@example.com
190Julie Dooleyjuliedooley@example.com
195John Deerejohn@deere.net

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

<?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 WHERE lastname LIKE 'D%'";
$result = $conn->query($sql);

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