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
this is my part of class code
Select Code
PHP Code:
private function jobs_by_category($category_id)
{
$category_id = $this->category_id;
$database = new Mysql_database();
$query = "SELECT * FROM jobs j ";
$query .= "INNER JOIN categories c ";
$query .= "ON j.category_id = c.id ";
$query .= "INNER JOIN company_owners co ";
$query .= "ON j.company_id = co.id ";
$query .= "WHERE category_id = '{$category_id}'";
i used return before in procedual code and it worked ok, now i can only echo in method, and i want to return values only inside the method to customize the layout later...
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. )
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
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
PHP Code:
function display_jobs_by_category()
{
$result = $this->jobs_by_category($this->category_id);
while ($row = mysql_fetch_array($result))
{
$job_info[] = $row;
}
return $job_info;
}
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.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks