Results 1 to 2 of 2

Thread: Database class query

  1. #1
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Database class query

    So I just started OOP yesterday and up until this point all my other functions worked well.
    However, I now have a problem where the query loops forever when using mysql_fetch_array.

    The function:
    PHP Code:
    function get_posts($forums=''$limit=''$order='')
            {
                if (
    $forums!=''){
                    
    $forums_value ' WHERE forum_id IN ('.$forums.')';
                }
                if (
    $order!=''){
                    
    $order_value ' ORDER BY '.$order;
                }
                if (
    $limit!=''){
                    
    $limit_value ' LIMIT '.$limit;
                }
                
                
    $this->query("SELECT * FROM ".$this->table_prefix."posts".$forums_value.$order_value.$limit_value."");
                return 
    $this->fetch_array();
            } 
    The query function:
    PHP Code:
    function query($query$cache=TRUE)
        {
            
    $this->numqueries++;
            if (
    $cache==TRUE)
            {
                
    $this->lastresult mysql_query($query);
                return 
    $this->lastresult;
            }
            else
            {
                return 
    mysql_query($query);
            }
            
        } 
    The fetch_array function:
    PHP Code:
    function fetch_array($result='')
        {
            
    $this->numqueries++;
            if (
    $result=='')
            {
                return 
    mysql_fetch_array($this->lastresult);
            }
            else {
                return 
    mysql_fetch_array($result);
            }
        } 
    Using the below in index.php:
    PHP Code:
    while ($row $bbsdk->get_posts()){
    echo 
    $row['time'].'<br />';

    Gives me:
    HTML Code:
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    1192627075
    etc..
    etc..
    There are 4 rows in the database table, and only 1 of them has the time: 1192627075.

    So for some reason, its looping forever but only getting the first row.

    Any help?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because you call query() in get_posts(), which resets the query each time the loop iterates. You're reinventing the wheel; I suggest looking into Propel.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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