1
votes

i'm new on PDO with PHP and i want your help. i made a function with a select statement and if the query has value greater than 0 it returns the fetch.

 $this->result->setFetchMode(PDO::FETCH_OBJ); 

 if ($this->result->rowCount() > 0) {     
   return $this->result->fetch(); 

 }

and on my main program i have this

 foreach ($category_results as $row){
        echo .$row['id_category'].' '. $row['name']; 

 }

this command echo only 1 and F twice but if i put only $row it echo all the results correct e.g 1 Food, 2 Drinks. how i can echo the results separately like the example i have above? i want to put them in a list box

thank you

1

1 Answers

3
votes

You are using:

 $this->result->setFetchMode(PDO::FETCH_OBJ); 

So you will have your rows stored in objects, also see the manual.

To get them, you can use:

 echo .$row->id_category.' '. $row->name; 

Also note that fetch only gets one row, to get all rows, use fetchAll. However, without seeing the complete code, I'm not sure whether you would need that.