PHP code example of liquidbox / silex-pdo
1. Go to this page and download the library: Download liquidbox/silex-pdo 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/ */
liquidbox / silex-pdo example snippets
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
'pdo.dsn' => 'mysql:host=localhost;dbname=test',
'pdo.username' => $user,
'pdo.password' => $passwd,
));
// or
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
'pdo.connection' => array(
'host' => 'localhost',
'dbname' => 'test'
),
'pdo.username' => $user,
'pdo.password' => $passwd,
));
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
'pdo.dsn' => 'sqlite::memory:',
));
// or
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array(
'pdo.driver' => 'sqlite',
'pdo.connection' => ':memory:',
));
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider('db', 'dbs'), array(
'pdo.driver' => 'pdo_pgsql',
'pdo.connection' => array(
'host' => 'localhost',
'dbname' => 'test',
),
'pdo.username' => $user,
'pdo.password' => $passwd,
));
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
foreach ($app['pdo']->query($sql) as $row) {
echo implode("\t", $row) . PHP_EOL;
}
/* Execute a prepared statement by passing an array of values */
$sql = <<<heredoc
SELECT name, color, calories
FROM fruit
WHERE calories < :calories AND color = :color
heredoc;
$pdoStatement = $app['pdo']->prepare($sql, array(
\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY,
));
$pdoStatement->execute(array(':calories' => 150, ':color' => 'red'));
$red = $pdoStatement->fetchAll();
$pdoStatement->execute(array(':calories' => 175, ':color' => 'yellow'));
$yellow = $pdoStatement->fetchAll();
$app->register(new LiquidBox\Silex\Provider\PdoServiceProvider(), array(
'pdo.dsn' => array(
'mysql_read' => array(
'connection' => array(
'host' => 'mysql_read.someplace.tld',
'dbname' => 'my_database',
'charset' => 'utf8mb4',
),
'username' => 'my_username',
'password' => 'my_password',
),
'mysql_write' => array(
'connection' => array(
'host' => 'mysql_write.someplace.tld',
'dbname' => 'my_database',
'charset' => 'utf8mb4',
),
'username' => 'my_username',
'password' => 'my_password',
),
),
));
$app['pdo']->query('SELECT * FROM table')->fetchAll();
$app['pdo.connections']['mysql_read']->query('SELECT * FROM table')->fetchAll();
$app->register(
new LiquidBox\Silex\Provider\PdoServiceProvider(null, 'pdo.dbs'),
array(
'pdo.dsn' => array(
'member_db' => array(
'driver' => 'pdo_pgsql',
'connection' => array(
'host' => 'member_data.someplace.tld',
'dbname' => 'membership',
),
'username' => $pgsql_user,
'password' => $pgsql_passwd,
),
'content_db' => array(
'connection' => array(
'host' => 'content_data.someplace.tld',
'dbname' => 'media_info',
'charset' => 'utf8',
),
'username' => $mysql_user,
'password' => $mysql_passwd,
),
'session_storage' => array('dsn' => 'sqlite::memory:'),
),
)
);
/* Execute a prepared statement by passing an array of values */
$pdoStatement = $app->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$pdoStatement->execute(array(150, 'red'));
$red = $pdoStatement->fetchAll();
$pdoStatement->execute(array(175, 'yellow'));
$yellow = $pdoStatement->fetchAll();