#1
  1. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    9
    Rep Power
    0

    Question "num_rows" in MS Access


    I am foolishly writing my own Database Abstraction Layer for both Access and MySQL.

    MySQL is quite easy, as most my functions parallel those built into PHP.

    MS Access is a different story. Particularly a "num_rows" function to count the number of rows returned by a SQL query.

    Before you tell me to use ODBC, I'm not using it! I'm using a COM object instead so I can keep the database and the PHP together. I want to be able to connect to my Access DB even if I don't have access on the server to make an ODBC connection. So the simple and easy solution is out.

    My current solution works fine when the query returns 2 or more rows. Basically it runs through a while loop and increments a counter. The problem is what happens if the query returns one or no rows. Here's an exerpt from the code:

    PHP Code:
        function num_rows () {
            if (
    is_object($this->COM_OBJ)) {
                if (
    is_object($this->CURSOR)) {
                    
    $this->CURSOR->movefirst();
                    
    $counter 0;
                    while (!(
    $this->CURSOR->EOF)){
                        
    $counter++;
                        
    $this->CURSOR->movenext();
                    }
                    
    $this->CURSOR->movefirst();
                    return 
    $counter;
                } else {
                    
    // There is no cursor from a query.
                    // Make sure to execute an SQL query using $obj->execute("sql_query)
                    
    $this->error ("There is no cursor from a SQL query"1);
                }                
            } else {
                
    // There is no database connection object.
                // Make sure to run $obj->connect("filename") before trying to collect data from a query.
                
    $this->error ("The database connection object is missing."1);
            }
        } 
    // END function num_rows () 
    Anyone out there have any good ideas how to do this better?

    Anyone know the way to get the number of rows directly from the Jet Engine through PHP? (That's the ideal solution.)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2002
    Posts
    693
    Rep Power
    26
    I have also wrote a cross-db class. I would recommend sticking to the basics. You really only need about 5 functions that will work with any DB.

    I really don't see the point of doing a count, It has to fetch all the rows and is inefficient, I would always do a select count(*)anyways.

    So what I believe to be the functions necessary are:
    1. open - connect to db and return handle
    2. execute - parse and execute query, return cursor
    3. fetch - move cursor forward
    4. getColumnResult - get value of column from cursor
    5. close - close db
  4. #3
  5. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    9
    Rep Power
    0
    yeah, I'm working around the problem by doing a
    PHP Code:
    SELECT count(*) FROM tablename 
    and storing that.

    What I'm trying to work toward is full functionality comprable to the built-in MySQL functions. It's probably a pipe dream in this case.

    That and using a SELECT count() is more to write. Then again, it does run quicker.

    That's why I want a one-liner that's built into the Jet DB Engine.

IMN logo majestic logo threadwatch logo seochat tools logo