I have scoured the web for a good example but I can't find anything.
I am trying to extend the mysqli class to make a helper class that will abstract away some of the complexities. One of the main things I want to accomplish is to make use of prepared statements.
I don't really know where to start, or how to handle input and output properly in one class. Another problem is that I am unable to output data as an array while using prepared statements.
I could really use a simple example to point me in the right direction.
Thanks!
check also Kohana Database Lib .. It pretty much does what you want : http://docs.kohanaphp.com/libraries/database
I've written a wrapper class to do this - it provides an identical interface for parameterised queries with either MySQLi or PDO. The interface allows you to execute parameterised SQL in a single line of code, which dramatically simplifies your own code.
http://www.robpoyntz.com/blog/?p=191
Check out Zend Framework and it's absolutely modular Zend_Db class, especially the mysqli adapter.
Check out the implementation of Zend_Db, and in particular,
Zend_Db_Select
. In fact, you might just opt to use that instead of developing your own. Examples:Assuming you're actually wanting to write your own version (as opposed to utilizing one of the existing libraries other answers have suggested - and those are good options, too)...
Here are a couple of functions which you may find it useful to examine. The first allows you to bind the results of a query to an associative array, and the second allows you to pass in two arrays, one an ordered array of keys and the other an associative array of data for those keys and have that data bound into a prepared statement: