SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fetching result from db queries (using a class)

    I'm trying to write a class to do the queries to db. This is what I have so far:
    PHP Code:
    class sqlQuery {
        var 
    $query_id 0;  
        var 
    $result   = array();

        function 
    query($sql) {
            
    $this->query_id mysql_query($sql)
                 or die(
    mysql_error());
            return 
    $this->query_id;
        }

        function 
    fetchArray() {
            
    $this->result mysql_fetch_array($this->query_id);
        }

        function 
    numRows() {
            return 
    mysql_num_rows($this->query_id);
        }    

    My problems start, when I'm trying to fetch the results trough a while-loop.
    This is my query (See the comments in the post to see what's working)
    PHP Code:
    $sql "SELECT line_id, line_name FROM valdres_line";
    $groups = new sqlQuery;
    $groups->query($sql);

    echo 
    $groups->numRows(); // THIS WORKS. Returns -9- which is correct

    while ($groups->fetchArray()) { // THIS WHILE-LOOP DOES NOT WORK
        
    echo $groups->result["line_id"];
        echo 
    ' - test<br />';

    Does anyone see what I'm doing wrong here?

    -Helge
    Last edited by Helge; Apr 2, 2002 at 06:54.

  2. #2
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function fetchArray() {
      
    $this->result mysql_fetch_array($this->query_id);
      
    // I think this line should be:
      // return mysql_fetch_array($this->query_id);


  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you husain for putting me on the right track!

    This is what I ended up with:
    PHP Code:
    function fetchArray() {
        
    $this->result mysql_fetch_array($this->query_id);
        return 
    $this->result;

    -Helge


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
  •