1$sql = "SELECT id, firstname, lastname FROM MyGuests";
2$result = $conn->query($sql);
3
4if ($result->num_rows > 0) {
5 // output data of each row
6 while($row = $result->fetch_assoc()) {
7 echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
8 }
9} else {
10 echo "0 results";
11}
12$conn->close();
1<?php
2// MySqli connection
3$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");
4
5// Errors
6if($mysqli->connect_error) {
7 echo "<b>Failed to connect to MySQL: </b>" . $mysqli->connect_error;
8}
9
10// Query
11$selectQuery = $mysqli->prepare("SELECT * FROM my_table");
12$selectQuery->execute();
13$selectQueryResult = $selectQuery->get_result();
14
15// Loop
16while($selectQueryRow = $selectQueryResult->fetch_array()) {
17 echo $selectQueryRow['my_column'];
18}
19
20// Example
21$selectUsername = $mysqli->prepare("SELECT * FROM users");
22$selectUsername->execute();
23$selectUsernameResult = $selectUsername->get_result();
24while($selectUsernameRow = $selectUsernameResult->fetch_array()) {
25 echo $selectUsernameRow['username'];
26}