PHP code example of busenov / database

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

    

busenov / database example snippets



// Let's assume you installed the library via composer
tting a "wrapper" object over mysqli - \busenov\Database\Mysql
$db = Mysql::create("localhost", "root", "password")
      // Error output language - English
      ->setErrorMessagesLang('en')
      // Database selection
      ->setDatabaseName("test")
      // Encoding selection
      ->setCharset("utf8")
      // Enable storage of all SQL queries for reporting/debugging/statistics
      ->setStoreQueries(true);

// Getting a result object \busenov\Database\Statement
// \busenov\Database\Statement - "wrapper" over an object mysqli_result
$result = $db->query("SELECT * FROM `users` WHERE `name` = '?s' AND `age` = ?i", "d'Artagnan", 41);

// We receive data (in the form of an associative array, for example)
$data = $result->fetchAssoc();

// SQL query not working as expected?
// Not a problem - print it and see the generated SQL query,
// which will already be with the parameters substituted into its body:
echo $db->getQueryString(); // SELECT * FROM `users` WHERE `name` = 'd\'Artagnan' AND `age` = 41


// Previously, before each request to the DBMS, we did
// something like this (and many people still don't do it):
$id = (int) $_POST['id'];
$value = mysqli_real_escape_string($mysql, $_POST['value']);
$result = mysqli_query($mysql, "SELECT * FROM `t` WHERE `f1` = '$value' AND `f2` = $id");

 $db->query("SELECT ?i", 123); 

 $db->query("SELECT ?i", '123'); 
 

// set strict mode
$db->setTypeMode(Mysql::MODE_STRICT);
// this expression will not be executed, an exception will be thrown:
// Trying to set placeholder type "int" to value type "double" in query template "SELECT ?i"
$db->query('SELECT ?i', 55.5);

$db->query('SELECT * FROM `users` WHERE `id` = ?i', $_POST['user_id']); 

$db->query('SELECT * FROM `prices` WHERE `cost` = ?d', 12.56); 

 $db->query('SELECT "?s"', "You are all fools, and I am d'Artagnan!");
 

 $db->query('SELECT "?S"', '% _'); 
 

 $db->query('SELECT ?n', 123); 
 

$db->query('INSERT INTO `test` SET ?Ai', ['first' => '123', 'second' => 1.99]);

 $db->query('SELECT * FROM `test` WHERE `id` IN (?ai)', [123, 1.99]);

$db->query('INSERT INTO `users` SET ?A[?i, "?s"]', ['age' => 41, 'name' => "d'Artagnan"]);

$db->query('SELECT * FROM `users` WHERE `name` IN (?a["?s", "?s"])', ['Daniel O"Neill', "d'Artagnan"]);

 $db->query('SELECT ?f FROM ?f', 'name', 'database.table_name');
 

$db->query('SELECT CONCAT("Hello, ", ?s, "!")', 'world');

$db->query('SELECT concat("Hello, ", "?s", "!")', 'world');