欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP轻量级数据库操作类Medoo增加、删除、修改、查询例子

程序员文章站 2022-04-10 21:29:39
...
Medoo介绍

Medoo是一款超轻量级的PHP SQL数据库框架,由社交网站Catfan和开源项目Qatrix的创始人黎言卓开发。提供了简单,易学,灵活的API,提升开发Web应用的效率与性能,而且体积只有8KB不到。

特性

轻量级,只有一个文件

简单易学,数据结构一目了然

支持多种SQL语法,以及支持复杂的查询条件

支持多种数据库,包括MySQL, MSSQL, SQLite等等

安全,可防止SQL注入

免费,基于MIT协议

示例代码

增加

复制代码 代码如下:


$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"
]
] );

删除

复制代码 代码如下:


$database = new medoo ( "my_database" );

$database->delete("account", [
"AND" => [
"type" => "business"
"age[ 18
]
]);

修改

复制代码 代码如下:


$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
] );

查询

复制代码 代码如下:


$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"] . "
";
}

// Select all columns
$datas = $database->select ( "account", "*" );

// Select a column
$datas = $database->select ( "account", "user_name" );

// $datas = array(
// [0] => "foo",
// [1] => "cat"
// )