I'm pretty new to PHP/SQL and I'm sure I'm making some simple mistake but I've searched for hours and cant figure out where it is. There's no errors being thrown when I test it.
Currently the below code just returns ---| as a result. to clarify the table im trying to get data from has 4 columns: name, room, in, out. it also has 6 rows of data that I can see is there in the database.
I'm trying to get it to return all of the data from the 6 rows that I have stored. Hopefully someone can show me the error of my ways
<?php
$db = new PDO('mysql:host=localhost;dbname=dbname','user','pass');
$sql = "SELECT * FROM table";
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $db->prepare($sql);
$stmt->execute();
$error= $stmt->errorInfo();
echo $error[2];
while($row = $stmt->fetch(PDO::FETCH_ASSOC));
{
echo $row['name']. "-" . $row['room']. "-" . $row['in']. "-" .$row['out']. "|";
}
?>
UPDATE:
as suggested i removed the while loop and replaced it with
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
it looks like its able to fetch the results just fine, didnt even have to view the source to see the results but im unsure why the while loop isnt working then. heres the output after replacing the while loop (only showing the result for 1 row, but all 6 rows are identical).
array(4) {
[0]=>
array(4) {
["name"]=>
string(4) "john"
["room"]=>
string(4) "room"
["in"]=>
string(1) "5"
["out"]=>
string(2) "11"
}