Last Updated: February 25, 2016
· entomb
Jonathan tavares

Simple MySQL wrapper using mysqli_*

Here's a simple mysql wrapper perfect for small scale applications such as cron jobs, facebook canvas campaigns or micro frameworks and sites.

Using the wrapper:

To start the db driver you must include the main class file and pass the '$config' array described bellow.
you can have multiple isntances of the Class each one with its own $config (one for Reads and one for Writes for example).

//include de main OBJ_mysql class file

//configuration array 
$config = array();
$config["hostname"]  = "YOUR_HOST";
$config["database"]  = "YOUR_DATABASE_NAME";
$config["username"]  = "USER_NAME";
$config["password"]  = "PASSWORD";

//class instantiation
$db = new OBJ_mysql($config);

Execute a query

$Result = $db->query("SELECT * FROM users");
$Users  = $Result->fetchAll();

binding parameters

$db->query("SELECT * FROM users WHERE id=?", array(3));

Insert secure data

$newUser = array( 
        'name'  => "jothn",
        'phnone' => "+351 123321123",
        'group' => 1,
        'active' => true,
  $new_user_id = $db->insert('users', $newUser);
        echo "new user id: $new_user_id";

You can find more information here:

Say Thanks

12 Responses
Add your response

Pro pic

@entomb awesome CLASS.Quite a useful one. Just forked it :)

over 1 year ago ·
Jonathan tavares

@kunalvarma05 Thanks! make sure to report back any bug/feature requests!

over 1 year ago ·
Pro pic

@entomb absolutely.

over 1 year ago ·

Why encapsulate an already encapsulated object? I appreciate you've added convenience methods. But several of the methods you've added are already exist (i.e. fetchAll). If you're primary goal is to decouple the DB platform, I'd advocate using PDO.

over 1 year ago ·
Jonathan tavares

@gonedark I understand you point, and its valid, but this class aims to be simpler than PDO, I wouldn't recomend using it for anything that requires the use of all database functionality.

on the other and, a small 2 file class performs better on small applications such as cronjobs or simple "registration" scrips or campaigns where there is no need to overkill the code with unecessary functionalities. It's all about the right tool for the job, and PDO (or any other dba you can find online) is powerfull and full or features it becomes complicated to use on small scale scripts.

As for the methods mapped, the class aims to spare you precious lines of code on pre-checks or input validations, as the native PHP driver trusts everything you send him, also, insert() update() and delete() methods provide a simple and straight forward way of interact with the DB impossible to achive with hand typed queries.

thanks for the feedback!

over 1 year ago ·
Pro pic

@entomb Your class is pretty solid one and a First-Pick for small-tier projects :)
I was wandering how to use a function as a condition in while loops, just like wordpress uses it in haveposts()
<?php while(have
echo $posttitle;
} ?>
The problem is, normally someone would list records using the while loop something like:
<?php while($row = mysql
echo $row['name'];
How can we make a solid class for the while loop? Where we can use methods as conditions and objects as records?

over 1 year ago ·
Jonathan tavares

Hey @kunalvarma05 !
There are diferent ways of doing it,
First, the fetch methods will return false if the recordset is empty, for example if you do somethign like:
if( $row=$result->fetch() ){ //something }
It will work only be true if fetch() returns anything.
another way is using loops, a while loop works the same way as the exaple above.
Assibe from this you have a method to check the record existance, try using:

if( $result->is_empty() ){ echo "result empty"; }else{ echo "record not empty"; }

for more info on this check the on github:

over 1 year ago ·
Pro pic

@entomb absolutely killer! I'll surely test it on my next project and notify you.
What's with the Aliases[$db->get()]?
There's a mistake over in your class's
It must be
$db->get()                  // Alias for $db->fetch(); </code> not $db->get()                  // Alias for $db->fetchAll();  </code>

over 1 year ago ·
Jonathan tavares

@kunalvarma05 You'r right! thanks, I updated the readme

over 1 year ago ·
Pro pic

@entomb I suppose you misunderstood. Besides pointing out the mistake, I also asked what are Aliases? What's the different between those methods and Aliases that you've listed?

over 1 year ago ·
Jonathan tavares

@kunalvarma05 Aliases are alternative names for methods, in a way that you can somehow dislike the "fetch" term and feel more confortable with "get", this depends from your experience with other db drivers, some use fetch, some use get, I figured I should offer both options.

over 1 year ago ·
Pro pic

@entomb cool :)

over 1 year ago ·