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

这篇文章主要介绍了PHP轻量级数据库操作类Medoo增加、删除、修改、查询例子,Medoo是一款超轻量级的PHP SQL数据库框架,支持MySQL, MSSQL, SQLite等数据库,需要的朋友可以参考下

Medoo介绍

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

特性

轻量级,只有一个文件

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

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

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

安全,可防止SQL注入

免费,基于MIT协议

示例代码

增加代码如下:

  1. $database = new medoo ( "my_database" );
  2. $last_user_id = $database->insert ( "account", [
  3. "user_name" => "foo",
  4. "email" => "foo@bar.com",
  5. "age" => 25,
  6. "lang" => [
  7. "en",
  8. "fr",
  9. "jp",
  10. "cn"
  11. ]
  12. ] );

删除代码如下:

  1. $database = new medoo ( "my_database" );
  2. $database->delete("account", [
  3. "AND" => [
  4. "type" => "business"
  5. "age[<]" => 18
  6. ]
  7. ]);

修改代码如下:

  1. $database = new medoo ( "my_database" );
  2. $database->update ( "account", [
  3. "type" => "user",
  4. // All age plus one
  5. "age[+]" => 1,
  6. // All level subtract 5
  7. "level[-]" => 5,
  8. "lang" => [
  9. "en",
  10. "fr",
  11. "jp",
  12. "cn",
  13. "de"
  14. ]
  15. ], [
  16. "user_id[<]" => 1000
  17. ] );

查询,代码如下:

  1. $database = new medoo ( "my_database" );
  2. $datas = $database->select ( "account", [
  3. "user_name",
  4. "email"
  5. ], [
  6. "user_id[>]" => 100
  7. ] );
  8. // $datas = array(
  9. // [0] => array(
  10. // "user_name" => "foo",
  11. // "email" => "foo@bar.com"
  12. // ),
  13. // [1] => array(
  14. // "user_name" => "cat",
  15. // "email" => "cat@dog.com"
  16. // )
  17. // )
  18. foreach ( $datas as $data ) {
  19. echo "user_name:" . $data ["user_name"] . " - email:" . $data ["email"] . "<br>";
  20. }
  21. // Select all columns
  22. $datas = $database->select ( "account", "*" );
  23. // Select a column
  24. $datas = $database->select ( "account", "user_name" );
  25. // $datas = array(
  26. // [0] => "foo",
  27. // [1] => "cat"
  28. // )