1. Go to this page and download the library: Download arrtxp/sql-driver 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/ */
arrtxp / sql-driver example snippets
use Arrtxp\SqlDriver\Adapter;
use Arrtxp\SqlDriver\Model;
$configDatabase = [
'driver' => 'Pdo',
'driver_options' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
],
'dsn' => 'mysql:dbname=test;host=localhost;charset=utf8mb4',
'dbname' => 'test',
'username' => 'test',
'password' => 'test',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
],
$adapter = new Adapter($configDatabase);
class User {
public int $id;
public string $name;
}
class Users extends Model {
public string $table = 'users';
}
$modelUser = new Users($adapter);
// insert
// INSERT INTO `users` (`name`) VALUES ('Test')
$userId = $modelUser
->insert()
->add(
[
'name' => 'Test',
]
)
->execute();
// get row
// SELECT `u`.* FROM `users` `u` WHERE `u`.`id` = 1
$user = $modelUser
->select()
->where('id', $userId)
->getRow(User::class);
// $user is instanceof User
// get rows
// SELECT `u`.* FROM `users` `u` WHERE 1
$users = $modelUser
->select()
->getRows(User::class);
foreach ($users as $user) {
// $user is instanceof User
}
// update row
// UPDATE `users` `u` SET `u`.`name` = 'Jan' WHERE `u`.`id` = 1
$modelUser
->update()
->set('name', 'Jan')
->where('id', $userId)
->execute();
// delete row
// DELETE `u` FROM `users` `u` WHERE `u`.`id` = 1
$modelUser
->delete()
->where('id', $userId)
->execute();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.