1

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yeap you have right. i put the featchall an then using the -> for the object is shows the results. thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.