www.webdeveloper.com
Results 1 to 5 of 5

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

  1. #1
    Join Date
    Dec 2012
    Posts
    7

    Question 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}'";
            
            
    $result $database->database_query($query);
            return 
    $result;
        } 
    this is mysql query code
    PHP Code:
        function display_jobs_by_category()
        {
            
    $result $this->jobs_by_category($this->category_id);
            while (
    $row mysql_fetch_array($result))
            {
                
                
    $job_category $row['category_title'];
                
    $job_title    =    $row['job_title'];
                
    $job_company =    $row['company_title'];

                return 
    $job_category.$job_title.$job_company;  //if i used echo here i get info from 
    all rows!!!! 
            }
        } 
    and the last thing here is my code to instiate an object
    PHP Code:
        if ($Job->company_name != '')
        {
            
    $job_by_company     =     $Job->display_jobs_by_company();
            echo 
    $job_by_company['title'];
        } 

    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...

    thanks for any help....

















  2. #2
    Join Date
    Dec 2012
    Posts
    7


    this is another method that i tried to return an array of data but also didn't work..
    PHP Code:
        function display_jobs_by_company()
        {
            
    $result $this->jobs_by_company($this->company_name);
            while (
    $row mysql_fetch_array($result))
            {
                
    $job_info = array("category"        =>    "{$row['category_title']}"
                
    ,"title"            =>    "{$row['job_title']}"
                
    ,"date"            =>    "{$row['job_date']}"
                
    ,"company"        =>    "{$row['company_title']}"
                
    ,"company_adress"    =>    "{$row['company_adress']}"
                
    ,"compny_phone"    =>    "{$row['company_phone']}"
                
    ,"company_mail"    =>    "{$row['company_mail']}"
                
    ,"salary"            =>    "{$row['job_salary']}"
                
    ,"discrebtion"    =>    "{$row['job_discreption']}");
                return 
    $job_info;
            }
        } 
















  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    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

    eBookworm.us

  4. #4
    Join Date
    Dec 2012
    Posts
    7

    Thumbs up

    Quote Originally Posted by NogDog View Post
    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

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    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

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles