1. Go to this page and download the library: Download mindplay/sql 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/ */
mindplay / sql example snippets
/**
* @property-read Column $id
* @property-read Column $first_name
* @property-read Column $last_name
*/
class UserTable extends Table
{
public function id($alias)
{
return $this->autoColumn(__FUNCTION__, IntType::class, $alias);
}
public function first_name($alias)
{
return $this->
$query = $db
->select($customer)
->columns([
$customer->id,
$customer->first_name,
$customer->last_name,
])
->value(
"SELECT COUNT({$order}) FROM {$order}"
. " WHERE ({$order->total} > :min_total)"
. " AND ({$order->customer_id} = {$customer->id})",
"num_orders"
)
->bind("min_total", 100);
$connection->execute(
$db->sql("DELETE FROM order WHERE id = :id")->bind("id", 123)
);
$delete = $db->sql("DELETE FROM order WHERE id = :id")->bind("id", 123);
if ($connection->execute($delete)->getRowsAffected() !== 1) {
// delete failed!
}
$query = $db->sql("SELECT * FROM user");
$result = $connection->fetch($query);
foreach ($result as $row) {
var_dump($row["id"], $row["first_name"]);
}
class UserMapper implements Mapper
{
public function map(array $rows)
{
foreach ($rows as $row) {
yield new User($row["id"], $row["first_name"], $row["last_name"]);
}
}
}
$query = $db
->select($user)
->where("{$user->name} LIKE :name")
->bind("name", "%rasmus%")
->page($page_no, 20); // $page_no is the base-1 page number
$count = $connection->count($query); // total number of matching results (for display)
$num_pages = ceil($count / 20); // total number of pages (for display)
$result = $connection->fetch($query); // 20 records of the requested page number
$connection->transact(function () use ($connection, $db) {
$connection->execute(
$db->sql("UPDATE payment WHERE id = :payment_id SET paid = NOW()")->bind(...)
);
$connection->execute(
$db->sql("INSERT INTO subscription (...) VALUES (...)")->bind(...);
);
return true; // COMMITS the transaction
});
$delete = $connection->prepare($db->sql("DELETE FROM order WHERE id = :id"));
foreach ($ids as $id) {
$delete->bind("id", $id);
$delete->execute();
}