PHP code example of tschoffelen / db.php

1. Go to this page and download the library: Download tschoffelen/db.php 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/ */

    

tschoffelen / db.php example snippets




$db = new Database($database_name, $username, $password, $host); // $host is optional and defaults to 'localhost'

$db->select($table, $where, $limit, $order, $where_mode, $select_fields)

// get the first 10 candy bars that are sweet, and order them by amount
$db->select('candy', ['sweet' => 1, 'spicy' => 0], 10, 'amount DESC');

// get the ids 1, 2,5,9  from products
$db->select('products', ['id' => 'in (1,2,5,9)'], false, false,'OR');

echo $db->select('candy', ['sweet' => 1], 10)->count();

$db->insert($table, $fields=[])

$db->insert(
	'candy', [
		'name' => 'Kitkat original',
		'sweet' => 1,
		'spicey' => 0,
		'brand' => 'Kitkat',
		'amount_per_pack' => 4
	]
);

$db->update($table, $fields=[], $where=[])

// set amount per pack to 5 for all Kitkats
$db->update(
	'candy', [
		// fields to be updated
		'amount_per_pack' => 5
	], [
		// 'WHERE' clause
		'brand' => 'Kitkat'
	]
);

$db->delete($table, $where=[])

// delete all Kitkat candy
$db->delete(
	'candy', [
		// 'WHERE' clause
		'brand' => 'Kitkat'
	]
);

$my_db = Database::instance();

// Global scope
$db = new Database($database_name, $username, $password, $host);

// Function scope
function something() {
    // We could simply use `global $db;`, but using globals is bad. Instead we can do this:
    $db = Database::instance();

    // And now we have access to $db inside the function
}