PHP code example of dcblogdev / pdo-wrapper

1. Go to this page and download the library: Download dcblogdev/pdo-wrapper 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/ */

    

dcblogdev / pdo-wrapper example snippets


//create table
$db->raw("CREATE TABLE demo (id int auto_increment primary key, name varchar(255))");

//use PDO directly
$db->getPdo()->query('Select username FROM users')->fetchAll();

//use run to query and chain methods
$db->run("SELECT * FROM users")->fetchAll();
$db->run("SELECT * FROM users")->fetch();
$db->run("SELECT * FROM users WHERE id = ?", [$id])->fetch();
//select using array instead of object
$db->run("SELECT * FROM users")->fetch(PDO::FETCH_ASSOC);

//get by id
$db->getById('users', 2);

//get all rows
$db->rows("SELECT title FROM posts");
//get all rows with placeholders
$db->rows("SELECT title FROM posts WHERE user_id = ?", [$user_id]);

//get single row
$db->row("SELECT title FROM posts");
//get single row with placeholders
$db->row("SELECT title FROM posts WHERE user_id = ?", [$user_id]);

//count
$db->count("SELECT id FROM posts");
$db->count("SELECT id FROM posts WHERE category_id = ?", [$category_id]);

//insert
$id = $db->insert('users', ['username' => 'Dave', 'role' => 'Admin']);

//last inserted id
$db->lastInsertId()();

//update
$db->update('users', ['role' => 'Editor'], ['id' => 3]);

//delete from table with a where claus and a limit of 1 record
$db->delete('posts', ['type_id' => 'draft'], $limit = 1);

//delete from table with a where claus and a limit of 10 record
$db->delete('posts', ['type_id' => 'draft'], $limit = 10);

//delete all from table with a where claus and a limit of 10 record
$db->delete('posts', ['type_id' => 'draft'], null);

//delete all from table
$db->deleteAll('posts');

//delete by id from table
$db->deleteById('posts', 2);

//delete by ids from table
$db->deleteById('posts', '2,4,7');

//truncate table
$db->truncate('posts');

use Dcblogdev\PdoWrapper\Database;

// make a connection to mysql here
$options = [
    //e' => 'mysql',
    'charset' => 'utf8',
    'host' => 'dev',
    'port' => '3309'
];

$db = new Database($options);

$db->getPdo()

$db->getPdo()->query($sql)->fetch();

$db->run("select * FROM users")->fetchAll();

$db->run("select * FROM users")->fetch();

$db->rows("select * FROM table");

$db->row("select * FROM table");

$db->row("select username FROM users WHERE id = :id and email = :email", ['id' => 1, ':email' => '[email protected]']);

$db->row("select username FROM users WHERE id = ? and email = ?", [1, '[email protected]']);

$rows = $db->rows("firstName, lastName FROM username ORDER BY firstName, lastName");
foreach ($rows as $row) {
    echo "<p>$row->firstName $row->lastName</p>";
}

$db->row("column FROM table where id=:id", ['id' => 23]);

$db->row("column FROM table where id=?", [23]);

$db->getById('users', $id);

$db->raw("CREATE TABLE IF NOT EXISTS users (
  id INT(11) NOT NULL AUTO_INCREMENT,
  firstName VARCHAR(255) NOT NULL,
  lastnName VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  PRIMARY KEY (id))"
);

$db->count('users');

$db->count('users', 'user_id');

$data = [
    'firstName' => 'Joe',
    'lastnName' => 'Smith',
    'email' => '[email protected]'
];
$db->insert('users', $data);

$id = $db->insert('users', $data);

$data = [
    'firstName' => 'Joe',
    'lastnName' => 'Smith',
    'email' => '[email protected]'
];
$where = ['id' => 2];
$db->update('users', $data, $where);

$update = [ 
	'data' => [
	    'firstName' => 'Joe',
	    'lastnName' => 'Smith',
	    'email' => '[email protected]'
	],
	'where' => [
        'id' => 2
    ]
];

$db->update('users', $update['data'], $update['where']);

$where = ['id' => 2];
$db->delete('users', $where);

$db->delete('users', $where, 10);  //delete 10 records matcing the where
$db->delete('users', $where, null); //delete all records matching the where

$db->deleteAll('users');

$db->deleteById('users', $id);

$db->deleteByIds('users', 'id', '4,5,6');

$db->truncate('users');