Solved

extend results from SQLite3

Posted on 2015-01-30
4
165 Views
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:-
<?php
    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
0
Comment
Question by:tonelm54
  • 2
4 Comments
 
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?
0
 

Author Comment

by:tonelm54
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.
0
 
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."
http://php.net/manual/en/class.sqlite3result.php#115891
0
 
LVL 33

Accepted Solution

by:
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++;
           $r->reset();
           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.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.htaccess 5 38
mysql between clause 2 25
Is there a limit to how many inner joins you should use? 4 33
error while installing php56 in redhat enterprise linux 20 24
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 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