Examples: Selecting guests using mySQLi Object-oriented

id: 1 - Name: Don James
id: 2 - Name: Don Doe
id: 4 - Name: John Doe
id: 6 - Name: Daisy Duck
id: 7 - Name: Julie Dooley
id: 8 - Name: Bob Marley
id: 9 - Name: Rita Marley
id: 10 - Name: Peter Tosh
id: 11 - Name: Tom Cat
id: 12 - Name: Woody Woodpecker
id: 13 - Name: Mighty Mouse
id: 14 - Name: Bob Marley
id: 15 - Name: Rita Marley
id: 16 - Name: Peter Tosh
id: 17 - Name: Tom Cat
id: 18 - Name: Woody Woodpecker
id: 19 - Name: Mighty Mouse
id: 20 - Name: John Deere
id: 21 - Name: Don James
id: 22 - Name: Donald Duck
id: 23 - Name: Daisy Duck
id: 24 - Name: Julie Dooley
id: 25 - Name: John Deer
id: 26 - Name: Don James
id: 27 - Name: John Deer
id: 28 - Name: John Deere
id: 29 - Name: John Deer
id: 30 - Name: Don James
id: 31 - Name: Donald Duck
id: 32 - Name: Daisy Duck
id: 33 - Name: Julie Dooley
id: 34 - Name: Tom Cat
id: 35 - Name: Woody Woodpecker
id: 36 - Name: Mighty Mouse
id: 37 - Name: Bob Marley
id: 38 - Name: Rita Marley
id: 39 - Name: Peter Tosh
id: 40 - Name: Don James
id: 41 - Name: Don James
id: 42 - Name: John Deer
id: 43 - Name: John Deere
id: 44 - Name: Don James
id: 45 - Name: Donald Duck
id: 46 - Name: Daisy Duck
id: 47 - Name: Julie Dooley
id: 48 - Name: Tom Cat
id: 49 - Name: Woody Woodpecker
id: 50 - Name: Mighty Mouse
id: 51 - Name: Bob Marley
id: 52 - Name: Rita Marley
id: 53 - Name: Peter Tosh
id: 54 - Name: John Deer
id: 55 - Name: John Deere
id: 56 - Name: Don James
id: 57 - Name: Don James
id: 58 - Name: Tom Cat
id: 59 - Name: Woody Woodpecker
id: 60 - Name: Mighty Mouse
id: 61 - Name: Donald Duck
id: 62 - Name: Daisy Duck
id: 63 - Name: Julie Dooley
id: 64 - Name: Bob Marley
id: 65 - Name: Rita Marley
id: 66 - Name: Peter Tosh
id: 67 - Name: John Deer
id: 68 - Name: John Deere
id: 69 - Name: Bob Marley
id: 70 - Name: Rita Marley
id: 71 - Name: Peter Tosh
id: 72 - Name: Donald Duck
id: 73 - Name: Daisy Duck
id: 74 - Name: Julie Dooley
id: 75 - Name: John Deere
id: 76 - Name: Tom Cat
id: 77 - Name: Woody Woodpecker
id: 78 - Name: Mighty Mouse
id: 79 - Name: Don James
id: 80 - Name: Bob Marley
id: 81 - Name: Rita Marley
id: 82 - Name: Peter Tosh
id: 83 - Name: John Deer
id: 84 - Name: Don James
id: 85 - Name: John Deer
id: 86 - Name: John Deere
id: 87 - Name: Don James
id: 88 - Name: Don James
id: 89 - Name: Tom Cat
id: 90 - Name: Woody Woodpecker
id: 91 - Name: Mighty Mouse
id: 92 - Name: Donald Duck
id: 93 - Name: Daisy Duck
id: 94 - Name: Julie Dooley
id: 95 - Name: Bob Marley
id: 96 - Name: Rita Marley
id: 97 - Name: Peter Tosh
id: 98 - Name: Tom Cat
id: 99 - Name: Woody Woodpecker
id: 100 - Name: Mighty Mouse
id: 101 - Name: Don James
id: 102 - Name: Bob Marley
id: 103 - Name: Rita Marley
id: 104 - Name: Peter Tosh
id: 105 - Name: John Deer
id: 106 - Name: John Deere
id: 107 - Name: Don James
id: 108 - Name: Donald Duck
id: 109 - Name: Daisy Duck
id: 110 - Name: Julie Dooley
id: 111 - Name: Bob Marley
id: 112 - Name: Rita Marley
id: 113 - Name: Peter Tosh
id: 114 - Name: Tom Cat
id: 115 - Name: Woody Woodpecker
id: 116 - Name: Mighty Mouse
id: 117 - Name: John Deere
id: 118 - Name: Don James
id: 119 - Name: Donald Duck
id: 120 - Name: Daisy Duck
id: 121 - Name: Julie Dooley
id: 122 - Name: John Deer
id: 123 - Name: Don James
id: 124 - Name: Bob Marley
id: 125 - Name: Rita Marley
id: 126 - Name: Peter Tosh
id: 127 - Name: Don James
id: 128 - Name: Donald Duck
id: 129 - Name: Daisy Duck
id: 130 - Name: Julie Dooley
id: 131 - Name: Don James
id: 132 - Name: John Deere
id: 133 - Name: Tom Cat
id: 134 - Name: Woody Woodpecker
id: 135 - Name: Mighty Mouse
id: 136 - Name: John Deer
id: 137 - Name: Don James
id: 138 - Name: Bob Marley
id: 139 - Name: Rita Marley
id: 140 - Name: Peter Tosh
id: 141 - Name: John Deere
id: 142 - Name: Tom Cat
id: 143 - Name: Woody Woodpecker
id: 144 - Name: Mighty Mouse
id: 145 - Name: Donald Duck
id: 146 - Name: Daisy Duck
id: 147 - Name: Julie Dooley
id: 148 - Name: Don James
id: 149 - Name: Don James
id: 150 - Name: Bob Marley
id: 151 - Name: Rita Marley
id: 152 - Name: Peter Tosh
id: 153 - Name: John Deer
id: 154 - Name: John Deere
id: 155 - Name: Tom Cat
id: 156 - Name: Woody Woodpecker
id: 157 - Name: Mighty Mouse
id: 158 - Name: Donald Duck
id: 159 - Name: Daisy Duck
id: 160 - Name: Julie Dooley
id: 161 - Name: Don James
id: 162 - Name: John Deer
id: 163 - Name: John Deere
id: 164 - Name: Don James
id: 165 - Name: Bob Marley
id: 166 - Name: Rita Marley
id: 167 - Name: Peter Tosh
id: 168 - Name: Tom Cat
id: 169 - Name: Woody Woodpecker
id: 170 - Name: Mighty Mouse
id: 171 - Name: Don James
id: 172 - Name: Donald Duck
id: 173 - Name: Daisy Duck
id: 174 - Name: Julie Dooley
id: 175 - Name: Tom Cat
id: 176 - Name: Woody Woodpecker
id: 177 - Name: Mighty Mouse
id: 178 - Name: John Deer
id: 179 - Name: John Deere
id: 180 - Name: Donald Duck
id: 181 - Name: Daisy Duck
id: 182 - Name: Julie Dooley
id: 183 - Name: Don James
id: 184 - Name: Bob Marley
id: 185 - Name: Rita Marley
id: 186 - Name: Peter Tosh
id: 187 - Name: Don James
id: 188 - Name: Donald Duck
id: 189 - Name: Daisy Duck
id: 190 - Name: Julie Dooley
id: 191 - Name: Don James
id: 192 - Name: Tom Cat
id: 193 - Name: Woody Woodpecker
id: 194 - Name: Mighty Mouse

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";
$result = $conn->query($sql);

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

$conn->close();
?>

Same example as above but displayed in a table.

IDNameEmail
1Don Jamesjohn@example.com
2Don Doejohn@example.com
4John Doejohn@deere.net
6Daisy Duckdaisyduck@example.com
7Julie Dooleyjuliedooley@example.com
8Bob Marleybobmarley@example.com
9Rita Marleyritamarley@example.com
10Peter Toshpetertosh@example.com
11Tom Cattc@example.com
12Woody Woodpeckerwoody@example.com
13Mighty Mousemighty@example.com
14Bob Marleybobmarley@example.com
15Rita Marleyritamarley@example.com
16Peter Toshpetertosh@example.com
17Tom Cattc@example.com
18Woody Woodpeckerwoody@example.com
19Mighty Mousemighty@example.com
20John Deerejohn@deere.net
21Don Jamesjohn@example.com
22Donald Duckdduckn@example.com
23Daisy Duckdaisyduck@example.com
24Julie Dooleyjuliedooley@example.com
25John Deerjohnd@example.com
26Don Jamesjohn@example.com
27John Deerjohnd@example.com
28John Deerejohn@deere.net
29John Deerjohnd@example.com
30Don Jamesjohn@example.com
31Donald Duckdduckn@example.com
32Daisy Duckdaisyduck@example.com
33Julie Dooleyjuliedooley@example.com
34Tom Cattc@example.com
35Woody Woodpeckerwoody@example.com
36Mighty Mousemighty@example.com
37Bob Marleybobmarley@example.com
38Rita Marleyritamarley@example.com
39Peter Toshpetertosh@example.com
40Don Jamesjohn@example.com
41Don Jamesjohn@example.com
42John Deerjohnd@example.com
43John Deerejohn@deere.net
44Don Jamesjohn@example.com
45Donald Duckdduckn@example.com
46Daisy Duckdaisyduck@example.com
47Julie Dooleyjuliedooley@example.com
48Tom Cattc@example.com
49Woody Woodpeckerwoody@example.com
50Mighty Mousemighty@example.com
51Bob Marleybobmarley@example.com
52Rita Marleyritamarley@example.com
53Peter Toshpetertosh@example.com
54John Deerjohnd@example.com
55John Deerejohn@deere.net
56Don Jamesjohn@example.com
57Don Jamesjohn@example.com
58Tom Cattc@example.com
59Woody Woodpeckerwoody@example.com
60Mighty Mousemighty@example.com
61Donald Duckdduckn@example.com
62Daisy Duckdaisyduck@example.com
63Julie Dooleyjuliedooley@example.com
64Bob Marleybobmarley@example.com
65Rita Marleyritamarley@example.com
66Peter Toshpetertosh@example.com
67John Deerjohnd@example.com
68John Deerejohn@deere.net
69Bob Marleybobmarley@example.com
70Rita Marleyritamarley@example.com
71Peter Toshpetertosh@example.com
72Donald Duckdduckn@example.com
73Daisy Duckdaisyduck@example.com
74Julie Dooleyjuliedooley@example.com
75John Deerejohn@deere.net
76Tom Cattc@example.com
77Woody Woodpeckerwoody@example.com
78Mighty Mousemighty@example.com
79Don Jamesjohn@example.com
80Bob Marleybobmarley@example.com
81Rita Marleyritamarley@example.com
82Peter Toshpetertosh@example.com
83John Deerjohnd@example.com
84Don Jamesjohn@example.com
85John Deerjohnd@example.com
86John Deerejohn@deere.net
87Don Jamesjohn@example.com
88Don Jamesjohn@example.com
89Tom Cattc@example.com
90Woody Woodpeckerwoody@example.com
91Mighty Mousemighty@example.com
92Donald Duckdduckn@example.com
93Daisy Duckdaisyduck@example.com
94Julie Dooleyjuliedooley@example.com
95Bob Marleybobmarley@example.com
96Rita Marleyritamarley@example.com
97Peter Toshpetertosh@example.com
98Tom Cattc@example.com
99Woody Woodpeckerwoody@example.com
100Mighty Mousemighty@example.com
101Don Jamesjohn@example.com
102Bob Marleybobmarley@example.com
103Rita Marleyritamarley@example.com
104Peter Toshpetertosh@example.com
105John Deerjohnd@example.com
106John Deerejohn@deere.net
107Don Jamesjohn@example.com
108Donald Duckdduckn@example.com
109Daisy Duckdaisyduck@example.com
110Julie Dooleyjuliedooley@example.com
111Bob Marleybobmarley@example.com
112Rita Marleyritamarley@example.com
113Peter Toshpetertosh@example.com
114Tom Cattc@example.com
115Woody Woodpeckerwoody@example.com
116Mighty Mousemighty@example.com
117John Deerejohn@deere.net
118Don Jamesjohn@example.com
119Donald Duckdduckn@example.com
120Daisy Duckdaisyduck@example.com
121Julie Dooleyjuliedooley@example.com
122John Deerjohnd@example.com
123Don Jamesjohn@example.com
124Bob Marleybobmarley@example.com
125Rita Marleyritamarley@example.com
126Peter Toshpetertosh@example.com
127Don Jamesjohn@example.com
128Donald Duckdduckn@example.com
129Daisy Duckdaisyduck@example.com
130Julie Dooleyjuliedooley@example.com
131Don Jamesjohn@example.com
132John Deerejohn@deere.net
133Tom Cattc@example.com
134Woody Woodpeckerwoody@example.com
135Mighty Mousemighty@example.com
136John Deerjohnd@example.com
137Don Jamesjohn@example.com
138Bob Marleybobmarley@example.com
139Rita Marleyritamarley@example.com
140Peter Toshpetertosh@example.com
141John Deerejohn@deere.net
142Tom Cattc@example.com
143Woody Woodpeckerwoody@example.com
144Mighty Mousemighty@example.com
145Donald Duckdduckn@example.com
146Daisy Duckdaisyduck@example.com
147Julie Dooleyjuliedooley@example.com
148Don Jamesjohn@example.com
149Don Jamesjohn@example.com
150Bob Marleybobmarley@example.com
151Rita Marleyritamarley@example.com
152Peter Toshpetertosh@example.com
153John Deerjohnd@example.com
154John Deerejohn@deere.net
155Tom Cattc@example.com
156Woody Woodpeckerwoody@example.com
157Mighty Mousemighty@example.com
158Donald Duckdduckn@example.com
159Daisy Duckdaisyduck@example.com
160Julie Dooleyjuliedooley@example.com
161Don Jamesjohn@example.com
162John Deerjohnd@example.com
163John Deerejohn@deere.net
164Don Jamesjohn@example.com
165Bob Marleybobmarley@example.com
166Rita Marleyritamarley@example.com
167Peter Toshpetertosh@example.com
168Tom Cattc@example.com
169Woody Woodpeckerwoody@example.com
170Mighty Mousemighty@example.com
171Don Jamesjohn@example.com
172Donald Duckdduckn@example.com
173Daisy Duckdaisyduck@example.com
174Julie Dooleyjuliedooley@example.com
175Tom Cattc@example.com
176Woody Woodpeckerwoody@example.com
177Mighty Mousemighty@example.com
178John Deerjohnd@example.com
179John Deerejohn@deere.net
180Donald Duckdduckn@example.com
181Daisy Duckdaisyduck@example.com
182Julie Dooleyjuliedooley@example.com
183Don Jamesjohn@example.com
184Bob Marleybobmarley@example.com
185Rita Marleyritamarley@example.com
186Peter Toshpetertosh@example.com
187Don Jamesjohn@example.com
188Donald Duckdduckn@example.com
189Daisy Duckdaisyduck@example.com
190Julie Dooleyjuliedooley@example.com
191Don Jamesjohn@example.com
192Tom Cattc@example.com
193Woody Woodpeckerwoody@example.com
194Mighty Mousemighty@example.com

Here is the code for accessing the database and display the information in a table.

<?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";
$result = $conn->query($sql);

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