1. Go to this page and download the library: Download liftkit/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/ */
liftkit / database example snippets
use LiftKit\Database\Connection\MySql;
use LiftKit\DependencyInjection\Container\Container;
use LiftKit\Database\Cache\Cache;
use PDO;
$connection = new MySql(
new Container,
new Cache,
new PDO('connectionString', 'username', 'password')
);
$results = $connection->query(
"
SELECT *
FROM tbl
"
);
$connection->query(
"
SELECT *
FROM tbl
WHERE col1 = ?
AND col2 = ?
",
[
'val1',
'val2',
]
);
// NOTE:
// Results are not loaded into memory. Instead they are
// wrapped by an object of the class
// \LiftKit\Database\Result\Result
$results = $connection->query(
"
SELECT *
FROM tbl
"
);
foreach ($results as $row) {
echo 'column "name" = ' . $row['name'] . PHP_EOL;
echo 'column "id" = ' . $row['id'] . PHP_EOL;
}
// DELETE tbl.*
// FROM tbl
// WHERE id = 1
$query->delete()
->from('tbl')
->whereEqual('id', 1)
->execute();
// SELECT *
// FROM tbl1
// WHERE
// ( SELECT COUNT(*)
// FROM tbl2
// WHERE tbl1.id = tbl2.tbl1_id
// ) = 1
$results = $query->select('*')
->from('tbl1')
->whereEqual(
$connection->createQuery()
->select($connection->createRaw('COUNT(*)'))
->from('tbl2')
->whereEqual(
'tbl1.id',
$connection->quoteIdentifier('tb2.tbl1_id')
),
1
)
->execute();
function getAllTblRows ()
{
return $connection->createQuery()
->select('*')
->from('tbl')
->execute();
}
// SELECT *
// FROM tbl
$results = getActiveTblRows();
function getAllTblRows (Query $inputQuery = null)
{
return $connection->createQuery()
->select('*')
->from('tbl')
->composeWith($inputQuery)
->execute();
}
function getActiveTblRows ()
{
$query = $connection->createQuery()
->whereEqual('active', 1);
return getAllTblRows($query);
}
// SELECT *
// FROM tbl
// WHERE active = 1
$results = getActiveTblRows();
use LiftKit\Database\Schema\Schema;
use LiftKit\Database\Schema\Table\Table;
// We'll get back to schemas in a moment
$table = new Table(
$connection,
new Schema($connection),
'tbl'
);
// SELECT *
// FROM tbl
$results = $tbl->getRows();
// SELECT *
// FROM tbl
// WHERE active = 1
$results = $table->getRows(
$connection->createQuery()
->whereEqual('active', 1)
);
// SELECT *
// FROM tbl
// WHERE id = 1
// LIMIT 1
$row = $table->getRow(1);
// 'val1'
echo $row['field1'];
// 'val2'
echo $row['field2'];