new to php & oop, can't get all rows from database only one row help plz

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

new to php & oop, can't get all rows from database only one row help plz

Hi all this is my 1st post here
i am new to php and specially to OOP, i am a search class to get info from data base, i.e by category id, the problem is that, if inside the method i tried to return an array of values, i only get info from one
row but if i used echo inside the method, i get info from all rows

Yep, return() completely exits that function, so your loop will only have one iteration, and return the values from the first row of the result set. If you want to return all the result rows, then you need to append them to a string or an array, then return() that after the loop, and then let the calling code use that returned array (or text) however it wants to.

(If instead of using the deprecated MySQL extension you instead switched to either MySQLi or PDO, you could use their fetchAll() methods to get the entire result set as a 2-D array without having to create your own loop -- along with other goodies including full OOP support. )

Yep, return() completely exits that function, so your loop will only have one iteration, and return the values from the first row of the result set. If you want to return all the result rows, then you need to append them to a string or an array, then return() that after the loop, and then let the calling code use that returned array (or text) however it wants to.

(If instead of using the deprecated MySQL extension you instead switched to either MySQLi or PDO, you could use their fetchAll() methods to get the entire result set as a 2-D array without having to create your own loop -- along with other goodies including full OOP support. )

Thanks alot for your help, you helped me alot, and this code did the trick

but i don't know any thing about mysqli or pdo, sorry just a new noop, i have some tutorials about mysqli but i thought that it will be complicated some how, i will read more in mysqli field, thanks again

Just keep plugging away and learning as much as you can. For the vast majority of us, it did not all come together right away. I'd recommend learning to use the PDO database extension -- maybe on your next project? Besides being object-oriented, it lets you use stored procedures with bound parameters so that you don't have to worry about escaping external values in your SQL, plus it works with most of the popular RDBMS's out there, so if you want to switch from MySQL to PostgreSQL, for example, the changeover can be quite simple.

Also, Matt Zandstra's book, PHP 5 Objects, Patterns, and Practice did a lot to help me grasp not just the "hows", but also the "whys" of OOP in PHP.