PHP code example of morphable / simple-database

1. Go to this page and download the library: Download morphable/simple-database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

morphable / simple-database example snippets




use \Morphable\SimpleDatabase;

$db = new SimpleDatabase($dsn, $user, $pass, $pdoOptions, function ($e) {
    // custom callback on query error, for instance to log failures
    die("error has occured " . $e->getMessage());
});

// execute raw query, returns PDOStatement
$stmt = $db->query("select ?", "1");
$stmt->fetch(\PDO::FETCH_COLUMN) // 1

// select
$select = $db->builder($table)
    ->select('`col1`, `col2`, `col3`')
    ->join('table on table.col1 = tale.col2')
    ->where('`col1` = ? and `col2` = ?', [$param1, $param2])
    ->orderBy('`col1` DESC')
    ->limit(5)
    ->offset(5)
    ->execute(); // returns QueryExecuter

$select->fetch(); // all rows
$select->fetchOne(); // first row
$select->fetchColumn(); // only possible when you only select 1 column
$select->getStatement(); // returns \PDOStatement

// insert
$insertId = $db->builder($table)
    ->insert([
        'col1' => $col1,
        'col2' => $col2,
        'col3' => $col3,
    ])
    ->execute()
    ->getLastInsertId();

// update
$db->builder($table)
    ->update([
        'col1' => $col1,
    ])
    // throws exception when you don't have a where clause
    ->where('`id` = ?', $id)
    ->execute();

// delete
$db->builder($table)
    ->delete()
    // throws exception when you don't have a where clause
    ->where('`id` = ?', $id)
    ->execute();