PHP Lightweight Database Operation Class Medoo Add Delete Modify Query Example
- 2021-07-07 06:35:20
- OfStack
Introduction to Medoo
Medoo is an ultra-lightweight PHP SQL database framework developed by Li Yanzhuo, founder of social networking site Catfan and open source project Qatrix. It provides a simple, easy-to-learn and flexible API, which improves the efficiency and performance of developing Web applications, and its volume is less than 8KB.
Characteristic
Lightweight, only 1 file
Simple and easy to learn, and the data structure is clear
Support for multiple SQL syntax and complex query conditions
Supports a variety of databases, including MySQL, MSSQL, SQLite, and so on
Safe to prevent SQL injection
Free, based on MIT protocol
Sample code
Increase
$database = new medoo ( "my_database" );
$last_user_id = $database->insert ( "account", [
"user_name" => "foo",
"email" => "foo@bar.com",
"age" => 25,
"lang" => [
"en",
"fr",
"jp",
"cn"
]
] );
Delete
$database = new medoo ( "my_database" );
$database->delete("account", [
"AND" => [
"type" => "business"
"age[<]" => 18
]
]);
Modify
$database = new medoo ( "my_database" );
$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
$database = new medoo ( "my_database" );
$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"] . "<br>";
}
// Select all columns
$datas = $database->select ( "account", "*" );
// Select a column
$datas = $database->select ( "account", "user_name" );
// $datas = array(
// [0] => "foo",
// [1] => "cat"
// )