Some times ago i wrote that simple class in PHP (keep in mind: it’s not compatible with PHP4) for talking with a MySQL database and now that I’ve used it a little bit in some projects i want to share the code with all of you
First of all let me say that probably you can use for all life the classical mysql functions and not suffer of any “damage” and obviously i use those functions inside my own class, but if you want to write code in a more “OO way” and a more readable code too, you may give a try to that class.
Oh, don’t worry, i don’t go mad if you don’t want or don’t like my class
So, let’s stop with that insane writing and let me introduce briefly how class work in a user perspective, you can read all the source code until you’ve downloaded the source file.
First of all we need to construct an object of type MySQLDB:
$database = new MySQLDB();
There are three possible parameters you can pass to the constructor: username, password and hostname for accessing the server; those parameters are respectively, in case you leave them empty, an empty string for the first and second and “localhost” for the third.
You can easily change that parameters later using methods like setUser(), setPassword() or setServer() but i think it’s easier to pass first and second to the constructor instead:
$database = new MySQLDB("username", "passoword");
Obviously i’m talking in the eventuality that your dmbs is located on localhost.
If username, password and hostname are correctly given to the object you can connect to the database:
$database->connect();
You can at any time change database doing something like that:
$database->disconnect();
$database->setUser("newuser");
$database->setPassword("newpassword");
$database->setServer("newserver");
$database->connect();
Yeah, maybe you can write few rows of code doing by yourself, but if you don’t need to change all parameters it can be cool.
When you’re connected you can access a database in that way:
$database->setDatabase("database");
For setting and executing a query:
$database->setQuery("QUERY ...");
$database->query();
Maybe i had had to advert you that some of this methods can throw exceptions, so, for your safety is better if you use try & catch blocks that my laziness prevents me to write right now in that examples ![]()
If you want to retrieve data from a select type of query or maybe know how much rows you deleted/updated and things like that you can use that powerful duo: getResultsNumber() and getData().
A couple of examples can explain how them work:
/* I've done an insert before and want to know if all was well */
if ( $database->getResultsNumber() == 0 ) {
print("Something bad happened!");
}
// I've done a select between multiple rows and now want to use the data collected
for ( $i = 0; $i < $database->getResultsNumber(); $i++ ) {
$temp = $database->getData();
/* do something with the object $temp that has an attribute for every column you put in the select */
}
No need to call a special method for closing connection or cleaning the state, the destructor makes all the work for you! ![]()
What can i say more ?
Maybe you can write yourself or download a better MySQL class, but as i said, i’ve wrote that class and i prefer to share code than lost it on a remote directory of my computer.
Last thing to say is that MySQLDB class is released under GNU GPL version 3 license and that you can write me, or contact me in the way you prefer, for every question/comment.








4 responses so far ↓
1 ryuujin // Mar 7, 2008 at 3:58 pm
Wow, it is what I need. I’ll surely use your anal hole class… oh yeah!
Fregno comunque :*
2 Tom // Apr 12, 2008 at 7:44 pm
Non male, la testerò.
Tom
3 isazi // May 5, 2008 at 1:03 am
Bhe, fammi sapere che ne pensi dopo il test
Ciao
4 MySQL class 0.2 | isazi's home // Aug 5, 2008 at 5:10 pm
[...] March I wrote a post about a simple class that I use when I need to communicate with a MySQL database in PHP 5. Now it’s time to release version 0.2 of that [...]
Leave a Comment