Where developers come to connect, share, build and be inspired.

33

Simple MySQL wrapper using mysqli_*

2101 views

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

Comments

  • Pro-pic
    kunalvarma05

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

  • Jonathan-tavares
    entomb

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

  • Pro-pic
    kunalvarma05

    @entomb absolutely.

  • Jason_mccreary_normal
    gonedark

    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.

  • Jonathan-tavares
    entomb

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

  • Pro-pic
    kunalvarma05

    @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(haveposts()){ echo $posttitle; } ?> The problem is, normally someone would list records using the while loop something like: <?php while($row = mysqlfetcharray($result)){ 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?

  • Jonathan-tavares
    entomb

    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

  • Pro-pic
    kunalvarma05

    @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 README.md It must be $db->get()                  // Alias for $db->fetch(); not $db->get()                  // Alias for $db->fetchAll(); 

  • Jonathan-tavares
    entomb

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

  • Pro-pic
    kunalvarma05

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

  • Jonathan-tavares
    entomb

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

  • Pro-pic
    kunalvarma05

    @entomb cool :)

Add a comment