1. Go to this page and download the library: Download squirrelphp/queries 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/ */
squirrelphp / queries example snippets
use Squirrel\Connection\Config\Mysql;
use Squirrel\Connection\PDO\ConnectionPDO;
use Squirrel\Queries\DBBuilder;
use Squirrel\Queries\DBInterface;
use Squirrel\Queries\DB\ErrorHandler;
use Squirrel\Queries\DB\MySQLImplementation;
// Create a squirrel connection
$connection = new ConnectionPDO(
new Mysql(
host: 'localhost',
user: 'user',
password: 'password',
dbname: 'mydb',
),
);
// Create a MySQL implementation layer
$implementationLayer = new MySQLImplementation($connection);
// Create an error handler layer
$errorLayer = new ErrorHandler();
// Set implementation layer beneath the error layer
$errorLayer->setLowerLayer($implementationLayer);
// Rename our layered service - this is now our database object
$db = $errorLayer;
// $db is now useable and can be injected
// anywhere you need it. Typehint it with
// \Squirrel\Queries\DBInterface
$fetchEntry = function(DBInterface $db): array {
return $db->fetchOne('SELECT * FROM table');
};
$fetchEntry($db);
// A builder just needs a DBInterface to be created:
$queryBuilder = new DBBuilder($db);
// The query builder generates more readable queries, and
// helps your IDE in terms of type hints / possible options
// depending on the query you are doing
$entries = $queryBuilder
->select()
->fields([
'id',
'name',
])
->where([
'name' => 'Robert',
])
->getAllEntries();
// If you want to add more layers, you can create a
// class which implements DBRawInterface and
$selectStatement = $db->select('SELECT fieldname FROM tablename WHERE restriction = ? AND restriction2 = ?', [5, 8]);
$firstRow = $db->fetch($selectStatement);
$db->clear($selectStatement);
$selectStatement = $db->select('SELECT fieldname FROM tablename WHERE restriction = 5 AND restriction2 = 8');
$firstRow = $db->fetchOne('SELECT fieldname FROM tablename WHERE restriction = ? AND restriction2 = ?', [5, 8]);
$allRows = $db->fetchAll('SELECT fieldname FROM tablename WHERE restriction = ? AND restriction2 = ?', [5, 8]);
$selectStatement = $db->select('SELECT `fufumama`,`b`.`lalala`,`a`.`setting_value` AS "result",(`a`.`setting_value`+`b`.`blabla_value`) AS "result2" FROM `blobs`.`aa_sexy` `a`,`blobs`.`aa_blubli` `b` LEFT JOIN `blobs`.`aa_blubla` `c` ON (`c`.`field` = `b`.`field5` AND `b`.`sexy` = ?) WHERE (`a`.`field` = `b`.`field`) AND `setting_id`=? AND `boring_field_name` IN (?,?,?,?) AND (`setting_value` = ? OR `setting_value2` = ?) GROUP BY `a`.`field` ORDER BY `a`.`field` DESC LIMIT 10 OFFSET 5 FOR UPDATE', [5,'orders_xml_override',5,3,8,13,'one','two']);
$rowsAffected = $db->change('DELETE FROM `users_names` WHERE `userId`=?', [13]);
$db->transaction(function() {
// Do queries in here as much as you want, it will all be one transaction
// and committed as soon as this function ends
});
$db->transaction(function() use ($db) {
$tableId = $db->insert('myTable', [
'tableName' => 'Henry',
], 'tableId');
// This still does exactly the same as in the previous example, because the
// function will be executed without a "new" transaction being started,
// the existing one just continues
$db->transaction(function() use ($db, $tableId)) {
// If this fails, then the error handler will attempt to repeat the outermost
// transaction function, which is what you would want / expect, so it starts
// with the Henry insert again
$db->update('otherTable', [
'tableId' => $tableId,
], [
'tableName' => 'Henry',
]);
});
});
$userIds = $dbBuilder
->select()
->field('userId')
->inTable('users')
->where([
'u.zipCode' => 33769,
])
->getFlattenedFields();
foreach ($userIds as $userId) {
// Do something which each $userId here
}
$user = $dbBuilder
->select()
->inTable('users')
->where([
'user_id' => $userId, // user_id must be equal to $userId
])
->getOneEntry();
// $user now contains all table column and values:
echo $user['user_id'];
$file = $dbBuilder
->select()
->inTable('files')
->where([
'file_id' => 33,
])
->getOneEntry();
// Use file_data in some way, like showing or writing it - it is a regular string
echo $file['file_data'];
$rowsAffected = $db->change('UPDATE sessions SET time_zone = \'Europe/Zurich\' WHERE session_id = \'zzjEe2Jpksrjxsd05m1tOwnc7LJNV4sV\'');
$rowsAffected = $db->change('UPDATE sessions SET time_zone = ? WHERE session_id = ?', [
'Europe/Zurich',
'zzjEe2Jpksrjxsd05m1tOwnc7LJNV4sV',
]);
sql
... WHERE (`user_id` BETWEEN ? AND ?) AND (`create_date` > ?) ...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.