1. Go to this page and download the library: Download mrjgreen/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/ */
mrjgreen / database example snippets
$factory = new \Database\Connectors\ConnectionFactory();
$connection = $factory->make(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
// Don't connect until we execute our first query
'lazy' => true,
// Set PDO attributes after connection
'options' => array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_EMULATE_PREPARES => true,
)
));
$connection->query("SELECT id, username FROM customers");
$factory = new \Database\Connectors\ConnectionFactory();
$resolver->setDefaultConnection('local');
// Returns the `local` connection
$resolver->connection();
$statement = $connection->query('SELECT * FROM users WHERE name = ?', array('John Smith'));
// PDOStatement
$statement->rowCount();
$statement->fetchAll();
$firstRow = $connection->fetch('SELECT * FROM users WHERE name = ?', array('John Smith'));
$allRows = $connection->fetchAll('SELECT * FROM users WHERE name = ?', array('John Smith'));
$firstColumnFirstRow = $connection->fetchOne('SELECT COUNT(*) FROM users WHERE name = ?', array('John Smith'));
$data = array(
'username' = 'jsmith',
'name' = 'John Smith'
);
$now = $connection->raw('NOW()');
$connection->table('users')->insertUpdate(
array('username' => 'jsmith', 'active' => $now), // Insert this data
array('active' => $now) // Or partially update the row if it exists
);
//insertOnDuplicateKeyUpdate() is an alias of insertUpdate
$pdoStatement = $mainServer->table('users')->query(); // Returns a PDOStatement (which implements the `Traversable` interface)
// Will be inserted in batches of 1000 as it reads from the rowset iterator.
$backupServer->table('users')->buffer(1000)->insertIgnore($pdoStatement);