PHP code example of itlessons / php-database

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

    

itlessons / php-database example snippets


use Database\Manager;

$manager = new Manager;

$manager->addConnection([
    'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
    'username' => 'root',
    'password' => null,
    'options' => [] // pdo driver options 
]);

$conn = $manager->getConnection();
$data = $conn->queryAll('select * from users where id in (?,?,?)', 1, 2, 3);
// $data -> [['id' => 1, 'name' => 'joe'], ...]

$conn->exec('insert into users (id, name) values (?, ?)', 1, 'joe'));

$conn->execBatch(file_get_contents('file_with_queries.txt'));

$data = $manager
            ->query('users')
            ->select('*')
            ->whereIn('id', [1,2,3])
            ->execute();

// $data -> [['id' => 1, 'name' => 'joe'], ...]


$data = $manager
            ->query('users')
            ->update(['votes' => 2])
            ->where('id', 1)
            ->toSql();
// $data -> ['update `user` set `votes` = ? where `id` = ?',[2,1]]
            
$data = $manager
          ->query('users')
          ->insert([
              ['email' => '[email protected]', 'votes' => 0],
              ['email' => '[email protected]', 'votes' => 1]
          ])->execute();