I'm now learning PDO I just followed the tutorial on Youtube. So here I have here a method from Db class and I added a new line PDO::FETCH_ASSOC because I want to try a while loop besides of foreach.
public function query($sql, $params = array()){
$this->_error = false;
$this->_count = 0;
if($this->_query = $this->_pdo->prepare($sql)){
#check if theres a parameter and bindvalue to sql statement
if(count($params)){
$x = 1;
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
#execute with or without parameter
if($this->_query->execute()){
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_whileFetch = $this->_query->fetch(PDO::FETCH_ASSOC);
$this->_count = $this->_query->rowCount();
}else{
$this->_error = true;
}
return $this;
}
} #end of GET function
public function whileFetch(){
return $this->_whileFetch;
}
The foreach is working well, but I want to try a while loop but it seems not working, not showing any data.
$query = $db->query("SELECT * from master_data.store");
while($row = $query->whileFetch()){
echo $row['name'];
}
I also try this, but i got error.
while($row = $query->fetch(PDO::FETCH_ASSOC)){
echo $row['name'];
}