PHP lightweight database operations class Medoo add, delete, modify, query example _ php instance

Source: Internet
Author: User
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"
//)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.