PHP code example of philiagus / pdostatementbuilder
1. Go to this page and download the library: Download philiagus/pdostatementbuilder 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/ */
philiagus / pdostatementbuilder example snippets
class SomeRepository
{
/**
* @type \PDO
*/
private $pdo;
function getSomething(array $ids, bool $someFilter = false, ?int $exlude = null): array
{
if (empty($ids)) {
return [];
}
$statement = "SELECT *
FROM `table`
WHERE id IN (?" . str_repeat(',?', count($ids) - 1) . ")";
$params = array_values($ids);
if ($someFilter) {
$statement .= " AND `filter_field` IS NOT NULL AND `filter_field2` = 1";
}
if ($exlude !== null) {
$statement .= ' AND `explude_column` = ?';
$params[] = $exlude;
}
$statement = $this->pdo->prepare($statement);
$statement->execute($params);
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
}
class SomeRepository
{
/**
* @type \PDO
*/
private $pdo;
function getSomething(array $ids, bool $someFilter = false, ?int $exclude = null): array
{
if (empty($ids)) {
return [];
}
$builder = new \Philiagus\PDOStatementBuilder\Builder();
$sql = $builder->build("
SELECT *
FROM table
WHERE id IN ({$builder->in($ids)})
/* {$builder->if($someFilter)} */
AND `filter_field` IS NOT NULL AND `filter_field2` = 1
/* {builder->endif()} */
/* {builder->if($exclude !== null)} */
AND `explude_column` = {$builder->value($exclude)}
/* {$builder->endif()} */
"
);
$statement = $sql->prepare($this->pdo);
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
}
class SomeRepository
{
/**
* @type \PDO
*/
private $pdo;
function getSomething(array $ids, bool $someFilter = false, ?int $exclude = null): array
{
if (empty($ids)) {
return [];
}
$builder = new \Philiagus\PDOStatementBuilder\Builder();
$sql = Builder::simple(
"SELECT * FROM `table` WHERE `id` = :id",
[':id' => 1]
);
$statement = $sql->prepare($this->pdo);
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.