Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

extend results from SQLite3

Posted on 2015-01-30
Last Modified: 2015-02-06
Im trying to write my own function into the resultset of an SQLite3 resultset. I want to create multiple classes that will work off the returned recordset (after query is fired).

My problem is, I dont know how to create a new instance of a resultset, and pass the current resultset into it.

My current idea was to do something like:-
    class testSQLite3 extends SQLite3 {
       public function query($sqlString) {
           $tmpRS = parent::query($sqlString); 
           $newRS = new testSQLite3Result()
           return $newRS;

    class testSQLite3Result extends SQLite3Result {
        function num_rows() {
            return 27;
  $sqlDB = new testSQLite3("test.db");
  $rs = $sqlDB->query("Select * from `myTable`;");
  echo $rs->num_rows();

Open in new window

But I dont know how to pass the resulting recordset into the new one.

Can anyone provide any pointers please?

Thank you
Question by:tonelm54
  • 2
LVL 109

Expert Comment

by:Ray Paseur
ID: 40580919
I looked at this earlier today and couldn't quite figure out what the "use case" would be.  Could you please rephrase the question, maybe in a non-technical terms?  For example, please tell us what the reasons are for extending SQLite3 and SQLite3Result.  Are there methods you need that you do not want to override?

Author Comment

ID: 40581165
Good morning,
Currently the only way of counting the number of rows returned in SQLite3 is to rephrase the SQL statment to do something like "select count(" (as far as I am aware), which does work but i find is just a bit of a mess. What Im trying to do is add the function num_rows() to the result set, so in this funtion i can just count the rows and return the number, just like you can in Mysqli result set.

I could create a new class independent to SQLite3 and pass the result set into that, however thought it would be tidier to expand the SQLite3Result class to include this statment, and others going forward.
LVL 109

Expert Comment

by:Ray Paseur
ID: 40581304
That makes sense.  Just a bit of philosophy... All of these data base wrappers (even the most advanced like the Eloquent ORM) suffer from one overriding issue - there are many things that we want to know about the contents of a data base, and so there are many ways of asking questions of a data base.  And this plethora of information makes it difficult to consolidate our thinking into a few neat and tidy methods.  Apparently the authors of SQLite were confused by the many options! :-)

Have a look at this solution.  It's clunky, but my instincts tell me that it may be as good as you can get without going "under the covers."
LVL 34

Accepted Solution

Slick812 earned 500 total points
ID: 40581697
greetings tonelm54, In your  "class testSQLite3"  code you have this -

class testSQLite3 extends SQLite3 {
       public function query($sqlString) {
           $tmpRS = parent::query($sqlString);
           $newRS = new testSQLite3Result()
           return $newRS;

as you can see the SQLite3 query will return a "SQLite3Result class" object, SO if you need to create an extension, and use this -
   $newRS = new testSQLite3Result();
your methods like fetchArray()  will NOT have the database DATA results in it from the SQLite3 query. For you to get your  fetchArray() to return you will need to pass the $tmpRS to your initializer, maybe like -
       $tmpRS = parent::query($sqlString);
       $newRS = new testSQLite3Result($tmpRS);
and have that stored as a reference, and used in EVERY method of yours maybe something like -
      public function fetchArray ($mode = SQLITE3_BOTH ) {
           return $this->Ores->fetchArray($mode);

This is not an easy thing to get correct, and trouble to DeBug, because the extended result object and the SQLite3 are LINKED to each other, although not directly, but through the database engine.

You can easily add properties to to an object so you might consider NOT extending the result, but just add a property.

class testSQLite3 extends SQLite3 {
       public function query($sqlString) {
           $r = parent::query($sqlString); 
           $r->num_rows = 0;
           while ($r->fetchArray(SQLITE3_NUM)) $r->num_rows++;
           return $r;

Open in new window

then you can just get the number o rows like
echo "number of rows i ".$result->num_rows;

Just a NOTE, you seem to be thinking in an OLD database way, as you think you need the number of rows, but In newer database access methods of SQLite3 (from the old SQLite2), I almost never need to get the number of rows until after a call to
    while ($result->fetchArray(SQLITE3_NUM))
where I can count the rows in that loop. If I just need row numbers I can do that in a SQL query.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
PHP and JQuery Syntax question 4 26
Row insertion failed. Array 5 46
Ahax pagination 9 32
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question