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}
1
2<?php
3$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
4foreach ($conn->query($sql) as $row) {
5 print $row['name'] . "\t";
6 print $row['color'] . "\t";
7 print $row['calories'] . "\n";
8}
9?>
10
11
1xPaw\SourceQuery\Exception\InvalidPacketException: Failed to read any data from socket in C:\xampp\htdocs\minecraft\vendor\xpaw\php-source-query-class\SourceQuery\BaseSocket.php:50
2Stack trace:
3#0 C:\xampp\htdocs\minecraft\vendor\xpaw\php-source-query-class\SourceQuery\Socket.php(76): xPaw\SourceQuery\BaseSocket->ReadInternal(Object(xPaw\SourceQuery\Buffer), 1400, Array)
4#1 C:\xampp\htdocs\minecraft\vendor\xpaw\php-source-query-class\SourceQuery\SourceQuery.php(212): xPaw\SourceQuery\Socket->Read()
5#2 C:\xampp\htdocs\minecraft\View.php(27): xPaw\SourceQuery\SourceQuery->GetInfo()
6#3 {main}