How to Use Medoo – A Lightweight PHP Database Framework Tutorial
This guide introduces Medoo, a lightweight PHP database framework built on PDO, outlines its key features, shows installation via Composer, demonstrates basic CRUD operations, and explains integration with the Webman framework, including multi‑database configuration and usage examples.
Overview
Medoo is a lightweight PHP database framework that provides a query builder based on PDO, supporting MySQL, PostgreSQL, SQLite and other databases.
Installation
composer require catfan/medooBasic usage
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use Medoo\Medoo;
$database = new Medoo([
'type' => 'mysql',
'host' => 'dnmp-mysql',
'database' => 'demo',
'username' => 'root',
'password' => '123456'
]);
// Insert a row
$database->insert('demo_user', [
'username' => 'Tinywan',
'mobile' => 1366936666
]);
// Select specific columns with a condition
$data = $database->select('demo_user', [
'username',
'mobile'
], [
'id' => 72
]);
echo json_encode($data);Result example:
[{"username":"Tinywan","mobile":"1366936666"}]Integration with Webman
Installation
composer require webman/medooConfiguration file
File path:
config/plugin/webman/medoo/database.phpSingle‑connection usage in a controller
<?php
namespace app\controller;
use support\Request;
use Webman\Medoo\Medoo;
class Index
{
public function index(Request $request)
{
$user = Medoo::get('user', '*', ['uid' => 1]);
return json($user);
}
}Multiple‑database configuration
Add additional connection arrays to config/plugin/webman/medoo/database.php. Example adds a connection named resty:
return [
'default' => [
'type' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
'prefix' => '',
'logging' => false,
'error' => PDO::ERRMODE_EXCEPTION,
'option' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL
],
'command' => [
'SET SQL_MODE=ANSI_QUOTES'
]
],
// Additional connection named "resty"
'resty' => [
'type' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
'prefix' => '',
'logging' => false,
'error' => PDO::ERRMODE_EXCEPTION,
'option' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL
],
'command' => [
'SET SQL_MODE=ANSI_QUOTES'
]
]
];Use the additional connection:
$user = Medoo::instance('resty')->get('user', '*', ['uid' => 1]);Reference
Official documentation URL: https://medoo.in/api/select
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
