Reputation: 111
I want to have all results from my database in one array
my function:
function GetWinkelProduct($g_Winkel) {
global $g_Conn;
$l_Stmt = $g_Conn->prepare("SELECT pd_id FROM `producten_:Winkel`");
$l_Stmt->bindValue(':Winkel', $g_Winkel, PDO::PARAM_INT);
$l_Stmt->execute();
$l_qurries = new dbquery();
$l_LastProduct = $l_qurries->GetLastProduct($g_Winkel);
while($l_Row = $l_Stmt->fetch(PDO::FETCH_ASSOC)){
$out = array();
foreach($l_Row as $product){
$out[] = $product['pd_id'];
}
}
return $out;
}
other page: <?php print_r($l_qurries->GetWinkelProduct($g_Winkel)); ?>
only I get the first result in the array and when I do $product['pd_id']
I get only the last result.
Upvotes: 0
Views: 447
Reputation: 1194
You must initialize the array outside the while loop and you don't need the foreach.
that way you are creating a new array every time the while iterates, that way you end up replacing the array.
$out = array();
while($l_Row = $l_Stmt->fetchAll(PDO::FETCH_ASSOC)){
$out[] = $l_Row['pd_id'];
}
return $out;
Upvotes: 0
Reputation: 5444
Try this...
Change fetch to fetchAll in while loop and renove foreach inside while loop
function GetWinkelProduct($g_Winkel) {
global $g_Conn;
$l_Stmt = $g_Conn->prepare("SELECT pd_id FROM `producten_:Winkel`");
$l_Stmt->bindValue(':Winkel', $g_Winkel, PDO::PARAM_INT);
$l_Stmt->execute();
$l_qurries = new dbquery();
$l_LastProduct = $l_qurries->GetLastProduct($g_Winkel);
while($l_Row = $l_Stmt->fetchAll(PDO::FETCH_ASSOC)){
$out = array();
$out[] = $l_Row ['pd_id'];
}
return $out;
}
ref:http://php.net/manual/en/pdostatement.fetchall.php
Upvotes: 2