itrn0/php-medoo-wrapper

A wrapper for the Medoo library that allows for easy fetching of rows.

v1.1.1 2023-02-14 10:55 UTC

This package is auto-updated.

Last update: 2024-04-14 13:58:33 UTC


README

This library extends Medoo's functionality with added methods for safer database interaction, including a SqlInterpolator class for safe variable interpolation and extended fetch methods.

Installation

You can install the library using composer:

composer require itrn0/php-medoo-wrapper

Usage

require __DIR__ . '/vendor/autoload.php';

use Itrn0\MedooWrapper\MedooWrapper;
use Itrn0\SqlInterpolator\SqlInterpolator;

$db = new MedooWrapper([
    'database_type' => 'mysql',
    'database_name' => 'test',
    'server' => 'localhost',
    'username' => 'root',
    'password' => ''
]);

$usernames = ['alice', 'bob'];
$res = $db->query(function (SqlInterpolator $interp) use ($usernames) {
    return <<<SQL
        SELECT * FROM users WHERE id IN ({$interp(...$usernames)})
    SQL;
});
$users = $res->fetchAll();

Fetching Data

You can fetch data using fetch and fetchAll methods.

// Fetch a single row from the query result set
$data = $db->fetch("SELECT * FROM `users` WHERE `name` = :name", [
    ':name' => 'John',
]);

// Returns an array containing all of the result set rows
$data = $db->fetchAll("SELECT * FROM `users`");

License

This library is licensed under the MIT License.