Reputation: 150
I want to fetch all the rows of a table but it only echos 1 row. (The latest) Tried much but nothing worked for me. Tried answers on stackoverflow but it is not working.
Here is the code that i use to fetch the rows:
$stmt = $pdo->prepare("SELECT * FROM admin");
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$gebruikersnaam = $row['gebruikersnaam'];
$wachtwoord = $row['wachtwoord'];
}
Hope that anyone can help me with this.
Upvotes: 1
Views: 109
Reputation: 319
$stmt = $pdo->prepare("SELECT * FROM admin");
$stmt->execute();
$result = $stmt->fetchAll();
$count = $stmt->rowCount();
for ($i=0; $i < $count; $i++) {
$gebruikersnaam = $result[$i]['gebruikersnaam'];
$wachtwoord = $result[$i]['wachtwoord'];
echo 'Gebruikersnaam is : '.$gebruikersnaam.'<br/>';
echo 'Wachtwoord is : '.$wachtwoord.'<br/>';
}
Or if you want to use foreach:
$stmt = $pdo->prepare("SELECT * FROM admin");
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$gebruikersnaam .= $row['gebruikersnaam'];
$wachtwoord .= $row['wachtwoord'];
}
echo 'Gebruikersnamen zijn : '.$gebruikersnaam.'<br/>';
echo 'Wachtwoorden zijn : '.$wachtwoord.'<br/>';
you have to style it al little bit because it'll stick them all together.
Upvotes: 0
Reputation: 46900
I want to fetch all the rows of a table but it only echos 1 row. (The latest)
foreach ($result as $row) {
$gebruikersnaam = $row['gebruikersnaam'];
$wachtwoord = $row['wachtwoord'];
}
I can bet 5 dollars on the assumption that you are using those variables outside the loop :) That's why you only see the last row's data. Either remove fetchAll()
and fetch()
them one by one or manipulate your variables within the loop so you get new values for every row.
Try this and you will know what i mean
foreach ($result as $row) {
print_r($row);
}
Upvotes: 1