Last Updated: May 31, 2021
·
6.968K
· entomb

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.

https://github.com/entomb/OBJ-MySQL

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
include("bin/OBJ_mysql.php");

//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);
  if($new_user_id){
        echo "new user id: $new_user_id";
  }

You can find more information here:
https://github.com/entomb/OBJ-MySQL

6 Responses
Add your response

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

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 ·

@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 ·

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 readme.md on github:
https://github.com/entomb/OBJ-MySQL/blob/master/README.md

over 1 year ago ·

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

over 1 year ago ·

@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 ·