Coder Social home page Coder Social logo

siquery's Introduction

siquery

siQuery is a simple SQL builder based on PDO for PHP

Usage

Include siquery class;

require_once 'src/siquery.php';

Select Statement

$db = new siquery();
$users = $db->from('users')
            ->orderBy('id DESC')
            ->get();

Executed Query :

SELECT *
FROM users
ORDER BY id DESC

Another example

$users = $db->select('username, email')
            ->from('users')
            ->orderBy('id DESC')
            ->get();

Executed Query :

SELECT username, email
FROM users
ORDER BY id DESC

Join Statement

$users = $db->from('users')
          ->innerJoin('posts', 'user_id')
          ->where('user_id',1)
          ->get();

Executed Query :

SELECT * 
FROM users
INNER JOIN posts
USING(user_id)
WHERE user_id = 1

Select first row

$users = $db->from('users')
          ->where('user_id', 1)
          ->first();

Select one column

$username = $db->from('users')
          ->where('user_id', 1)
          ->fetch('username');

Support paginate

$users = $db->from('users')
          ->paginate(20);

HTML for pagination will generate by function $db->getPagenav(); , so you can use it for pagination link in your view

Search

$books = $db->from('books')
          ->search('title, author')
          ->get();

It will use $_GET['search'] variable by default to get a keyword for search, you can set search variable like this

$db = new siquery();
$db->searchVar = 'MySearchVariable';

##Data Manipulation Languange ###Insert

$insert = $db->insert('users', ['username'=>'John', 'email'=>'[email protected]'])
          ->execute();

###Update

$update = $db->update('users', ['username'=>'Dale', 'email'=>'[email protected]'])
          ->where('user_id', 2)
          ->execute();

###Delete

$delete = $db->delete('users')
          ->where('user_id', 2)
          ->execute();

or

$delete = $db->delete('users', 'user_id = 2')
          ->execute()

Transaction

###Example :

$db = new siquery();

$db->TRANSACTION(); //Begin Transaction

  $insert = $db->insert('users', ['username' => 'John'])
            ->execute();
  
  $lastId = $db->lastId();
  
  $update = $db->update('books', ['user_id' => $lastId])
              ->where('books_id', 4)
              ->execute();

if(empty($db->getError())){
    
    $db->COMMIT(); //Commit transaction

} else {

    $db->ROLLBACK(); //Rollback transaction

}

siquery's People

Contributors

dimasdwib avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.