Home > Article > Backend Development > PHP lightweight database operation class Medoo add, delete, modify, query examples_PHP tutorial
Introduction to Medoo
Medoo is an ultra-lightweight PHP SQL database framework developed by Li Yanzhuo, the founder of the social networking site Catfan and the open source project Qatrix. It provides a simple, easy-to-learn, and flexible API to improve the efficiency and performance of developing web applications, and the size is less than 8KB.
Features
Lightweight, only one file
Easy to learn, the data structure is clear at a glance
Supports multiple SQL syntaxes and complex query conditions
Supports multiple databases, including MySQL, MSSQL, SQLite, etc.
Secure to prevent SQL injection
Free, based on MIT license
Sample code
Added
$last_user_id = $database->insert ( "account", [
"user_name" => "foo",
"email" => "foo@bar.com",
"age" => 25,
"lang" => [
"en",
"fr",
"jp",
"cn"
]
] );
Delete
Modify
$database->update ( "account", [
"type" => "user",
// All age plus one
"age[+]" => 1,
// All level subtract 5
"level[-]" => 5,
"lang" => [
"en",
"fr",
"jp",
"cn",
"de"
]
], [
"user_id[<]" => 1000
] );
Query
$datas = $database->select ( "account", [
"user_name",
"email"
], [
"user_id[>]" => 100
] );
// $datas = array(
// [0] => array(
// "user_name" => "foo",
// "email" => "foo@bar.com"
// ),
// [1] => array(
// "user_name" => "cat",
// "email" => "cat@dog.com"
// )
// )
foreach ( $datas as $data ) {
echo "user_name:" . $data ["user_name"] . " - email:" . $data ["email"] . "
";
}
// Select all columns
$datas = $database->select ( "account", "*" );
// Select a column
$datas = $database->select ( "account", "user_name" );
// $datas = array(
// [0] => "foo",
// [1] => "cat"
// )