MySQL-RS

I did a page on my blog where you can see the source of my MySQL resultset class that acts like an array by implementing some (3) SPL (Standard PHP Library) interfaces. I changed the code a little bit since yesterday (now it really works, except for the count() method as it’ll be available with PHP 5.1) so for those of you who were present at the PHPQuébec meeting in Drummondville should notice some changes.

My MySQL-rs class acts a an array (as said previously) but you can’t add any data nor delete data from it as it is read-only. This is version 1.0.0 so maybe I’ll do some changes in the future to allow you to add and delete data from it, but as for v1, it won’t change.

For those of you who actually want to learn how to work with the PHP5’s SPL, you can take a look at my class as well as those documentations : 1 2.
I did this in about 30 minutes or so, so don’t look how ugly my code is.

Here’s a little example how to use it :
[php]< ?php $query = mysql_query('SELECT * FROM my_table;'); $resultset = new mysqlrs($query); // if I want the first result entry in my table var_dump($resultset[0]); //var_dump because it returns an associative array where the keys are the column names of my table I fetch the data ?>[/php]
And that’s it !

Enjoy! And if you haven’t made the switch to PHP5 yet… you know what to do!
Happy coding with the SPL.