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
ID | Name | |
---|---|---|
2 | Don Doe | john@example.com |
4 | John Doe | john@deere.net |
6 | Daisy Duck | daisyduck@example.com |
7 | Julie Dooley | juliedooley@example.com |
20 | John Deere | john@deere.net |
22 | Donald Duck | dduckn@example.com |
23 | Daisy Duck | daisyduck@example.com |
24 | Julie Dooley | juliedooley@example.com |
25 | John Deer | johnd@example.com |
27 | John Deer | johnd@example.com |
28 | John Deere | john@deere.net |
29 | John Deer | johnd@example.com |
31 | Donald Duck | dduckn@example.com |
32 | Daisy Duck | daisyduck@example.com |
33 | Julie Dooley | juliedooley@example.com |
42 | John Deer | johnd@example.com |
43 | John Deere | john@deere.net |
45 | Donald Duck | dduckn@example.com |
46 | Daisy Duck | daisyduck@example.com |
47 | Julie Dooley | juliedooley@example.com |
54 | John Deer | johnd@example.com |
55 | John Deere | john@deere.net |
61 | Donald Duck | dduckn@example.com |
62 | Daisy Duck | daisyduck@example.com |
63 | Julie Dooley | juliedooley@example.com |
67 | John Deer | johnd@example.com |
68 | John Deere | john@deere.net |
72 | Donald Duck | dduckn@example.com |
73 | Daisy Duck | daisyduck@example.com |
74 | Julie Dooley | juliedooley@example.com |
75 | John Deere | john@deere.net |
83 | John Deer | johnd@example.com |
85 | John Deer | johnd@example.com |
86 | John Deere | john@deere.net |
92 | Donald Duck | dduckn@example.com |
93 | Daisy Duck | daisyduck@example.com |
94 | Julie Dooley | juliedooley@example.com |
105 | John Deer | johnd@example.com |
106 | John Deere | john@deere.net |
108 | Donald Duck | dduckn@example.com |
109 | Daisy Duck | daisyduck@example.com |
110 | Julie Dooley | juliedooley@example.com |
117 | John Deere | john@deere.net |
119 | Donald Duck | dduckn@example.com |
120 | Daisy Duck | daisyduck@example.com |
121 | Julie Dooley | juliedooley@example.com |
122 | John Deer | johnd@example.com |
128 | Donald Duck | dduckn@example.com |
129 | Daisy Duck | daisyduck@example.com |
130 | Julie Dooley | juliedooley@example.com |
132 | John Deere | john@deere.net |
136 | John Deer | johnd@example.com |
141 | John Deere | john@deere.net |
145 | Donald Duck | dduckn@example.com |
146 | Daisy Duck | daisyduck@example.com |
147 | Julie Dooley | juliedooley@example.com |
153 | John Deer | johnd@example.com |
154 | John Deere | john@deere.net |
158 | Donald Duck | dduckn@example.com |
159 | Daisy Duck | daisyduck@example.com |
160 | Julie Dooley | juliedooley@example.com |
162 | John Deer | johnd@example.com |
163 | John Deere | john@deere.net |
172 | Donald Duck | dduckn@example.com |
173 | Daisy Duck | daisyduck@example.com |
174 | Julie Dooley | juliedooley@example.com |
178 | John Deer | johnd@example.com |
179 | John Deere | john@deere.net |
180 | Donald Duck | dduckn@example.com |
181 | Daisy Duck | daisyduck@example.com |
182 | Julie Dooley | juliedooley@example.com |
188 | Donald Duck | dduckn@example.com |
189 | Daisy Duck | daisyduck@example.com |
190 | Julie Dooley | juliedooley@example.com |
195 | John Deere | john@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(); ?>