This article describes how to add, delete, modify, and query PHP lightweight database operations. Medoo is an ultra-lightweight PHPSQL database framework that supports databases such as MySQL, MSSQL, and SQLite, for more information, see
Medoo introduction
Medoo is a lightweight php SQL database framework developed by Li Yanzhuo, a social networking website Catfan and founder of Qatrix, an open-source project. It provides simple, easy to learn, and flexible APIs to improve the efficiency and performance of Web application development, and the size is less than 8 kB.
Features
Lightweight, with only one file
Easy to learn and clear data structure
Supports multiple SQL syntaxes and complex query conditions.
Supports a variety of databases, including MySQL, MSSQL, SQLite, etc.
Secure and prevents SQL injection
Free, based on the MIT protocol
Sample code
Add
The code is as follows:
$ 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
The code is as follows:
$ Database = new medoo ("my_database ");
$ Database-> delete ("account ",[
"AND" => [
"Type" => "business"
"Age [<]" => 18
]
]);
Modify
The code is as follows:
$ 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
The code is as follows:
$ Database = new medoo ("my_database ");
$ Datas = $ database-> select ("account ",[
"User_name ",
"Email"
], [
"User_id [& gt;]" = & gt; 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"
//)