Databases 4 min read

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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Use Medoo – A Lightweight PHP Database Framework Tutorial

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/medoo

Basic 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/medoo

Configuration file

File path:

config/plugin/webman/medoo/database.php

Single‑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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCRUDComposerPDOWebmanMedoo
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.