Tutorial :Looking for a simple mySQLi class example with descriptions



Question:

I am looking for an example of a simple, but functional example of a class extending the mySQLi class in PHP5. I am interested in stored procedures also. Basically what I need is an example of a practical and usable class. I learn best by example and unfortunately I have been unable to find any site or book that seems to have a really solid yet simple example. All the examples I see are way to complicated so I am unable to understand or they are so simple that I might as well just instantiate the class right then and there inline. It would be great if somebody could supply me with an example like this and explain whats going on however well they can. My long term goal is to have a class that I can instantiate that deals with authentication of the database and then takes my SQL statements and supplies the output in variables.

If anyone is willing to take the time to help me out it would be much appreciated! ,Thanks


Solution:1

Did you check PHP PEAR MDB2 ? http://pear.php.net/package/MDB2

Its not so simple, its the best DAL in php IMHO...


Solution:2

About mysqli, you can take a look at those articles :

(Quite old, but mysqli extension has not changed that much for a couple of years, so those should still be OK)

You could also have a look at PDO, included in PHP since version 5.1 ; it's an object-oriented API to access multiple DB engines (you don't use mysqli_query, pg_query, oci8_query or stuff like that ; only $pdo->query -- well, your SQL has to be compatible, but that's another problem ;-( )

Another approach would be using an ORM, like, for instance, Doctrine.

It's not a class that extends MySQLi, but a full Framework to abstract the DB engine - with that, you write a schema to describe you DB's structure, and you then use a "pseudo-SQL" language to send queries ; or just (for most cases) the object-oriented API that abstracts SQL.

It's a bit difficult to use, at first, but really powerful and useful (and not bad for productivity, I think, for many kind of projects)


Solution:3

give a try to DALMP: http://code.google.com/p/dalmp/ is support prepared statements and many cache backends


Solution:4

Little bit off topic but why MySQLi? PDO is much better and considered a defacto PHP standard these days. I wrote a simple PDO class a year or two ago, I can give you link if interested.


Solution:5

Here is a simple example of adding built in query timing to the mysqli class.

This assumes you have defined a "MyTimer" class implementing the iTimer interface. iTimer class, in it's simplest form, would record the start and stop times for the specified $key (using microtime() or getrusage() or whatever).

<?php  interface iTimer  {      public function start($key);      public function stop($key);  }    class mysqlp extends mysqli  {      function query($query, $resultmode)      {          $timer = MyTimer::getInstance();          $timer->start('mysqli::query');          $result = parent::query($query, $resultmode);          $timer->stop('mysqli::query');          return $result;      }  }    #usage  $mysqli = new mysqlp("localhost", "my_user", "my_password", "world");  $results = $mysqli->query("SELECT id FROM foo");  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »