I have a table in MYSQL called guest
. There are 3 columns in the guest
table GuestId
, Name
, Phone
. I want to select all records from the guest
table and put it into array variables, then I just want to get the phone value from array variables using (for) loop. I have tried using (for) loop then inside the loop I use :
$phone = $row[phone];
but the result I get is just the value phone of the first record. How to make the phone variables take the phone value from each record in every looping?
Thank you.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT GuestId, Name, Phone FROM guest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Phone: " . $row["Phone"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>