laitimes

Medoo Lightweight PHP Database Framework

author:Not bald programmer
Medoo Lightweight PHP Database Framework

overview

Medoo is a lightweight PHP database framework that provides a simple and easy-to-use query builder that allows developers to interact with the database in an elegant way. Medoo uses PDO extensions to provide a database abstraction layer that supports multiple database systems such as MySQL, PostgreSQL, SQLite, and more.

peculiarity

  1. Concise syntax: Medoo provides a concise query-building syntax that makes writing SQL queries intuitive and easy.
  2. Chained operations: Chained calls are supported, making the code more concise and easy to read.
  3. Type safety: Medoo provides good type safety through the parameter binding of PDOs.
  4. Cross-database support: Multiple database types are supported, allowing developers to easily switch database systems.
  5. Easy to extend: Medoo's architecture is designed to allow developers to extend and customize as needed

Basic Usage

Installation

composer require catfan/medoo           

use

<?php
/**
 * @desc medoo.php 描述信息
 * @date 2024/6/29 17:42
 */
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'
]);

$database->insert('demo_user', [
    'username' => 'Tinywan',
    'mobile' => 1366936666
]);

$data = $database->select('demo_user', [
    'username',
    'mobile'
], [
    'id' => 72
]);

echo json_encode($data);           

printout

[{"username":"layne","mobile":"xxxxxxxxxxx"}]           

webman uses

Installation

Install the medoo plug-in package

composer require webman/medoo           

arrangement

The configuration file is located in config/plugin/webman/medoo/database.php

use

<?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);
    }
}           

Multi-database configuration

config/plugin/webman/medoo/database.php adds a new configuration, key is arbitrary, and resty is used here.

<?php
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'
        ]
    ],
    // 这里新增了一个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

$user = Medoo::instance('resty')->get('user', '*', ['uid' => 1]);           

brief summary

Medoo is suitable for projects that require rapid development and simple database operations. If you're looking for a lightweight and powerful PHP database framework, Medoo might be a great choice.

Medoo Official Documentation: https://medoo.in/api/select

Read on