**使用Medoo數據庫操作類**
具體用法請查看。
https://medoo.in/
數據庫配置在`config.ini.php`中。
在使用時可直接
~~~
db()->select(table,field,where)
~~~
如 https://medoo.in/api/select
不同的是這里`db()` 是已經初始化數據庫對象了。
**讀取表中的字段**
~~~
get_table_fields($table)
~~~
**返回數據庫允許的數據,傳入其他字段自動忽略**
此方法常用于過濾掉表不需要的字段。
~~~
$data = db_allow($table,$data);
~~~
顯示數據庫表結構,支持markdown格式
~~~
database_tables($name = null,$show_markdown = false)
~~~
**分頁查尋**
~~~
JOIN
$where = [
//"do_order.id"=>1,
'ORDER'=>[
'do_order.id'=>'DESC'
]
];
int date
$where['printer_refund_apply.created_at[<>]'] = [
$dates[0] / 1000, $dates[1] / 1000
];
datetime
$where['printer_refund_apply.created_at[<>]'] = [
date('Y-m-d H:i:s',$dates[0] / 1000), date('Y-m-d H:i:s',$dates[1] / 1000)
];
$data = db_pager("do_order",
["[><]do_mini_user" => ["uid" => "id"]],
[
"do_order.id",
"do_order.uid",
"user" => [
"do_mini_user.nickName",
"do_mini_user.avatarUrl",
"do_mini_user.openid",
]
],
$where);
~~~
**根據表名、字段 、條件 查尋多條記錄**
~~~
db_get($table, $join = "*", $columns=null, $where=null)
~~~
**寫入記錄**
~~~
db_insert($table, $data = [])
~~~
**更新記錄**
~~~
db_update($table, $data = [], $where = [])
~~~
**數據庫事務**
~~~
action($call)
或
$db = db();
$db->action(function ($db) use (&$result, $call) {
});
~~~
**根據表名、字段 、條件 查尋一條記錄**
~~~
db_get_one($table, $join = "*", $columns=null, $where=null)
~~~
**執行SQL**
~~~
db_query($sql,$raw=null) //$raw有參數時為數組傳值
~~~
如
~~~
db_query("select * from user where user=:user",[":user"=>'admin'])
~~~
**取最小值**
~~~
db_get_min($table, $join = "*", $column=null, $where=null)
~~~
**取最大值**
~~~
db_get_max($table, $join = "*", $column = null, $where = null)
~~~
**總數**
~~~
db_get_count($table, $join = "*", $column = null, $where = null)
~~~
是否有記錄
~~~
db_get_has($table, $join = null, $where = null)
~~~
隨機取多條記錄
~~~
db_get_rand($table, $join= "*", $column=null, $where=null)
~~~
取總和
~~~
db_get_sum($table, $join="*", $column=null, $where=null)
~~~
取平均值
~~~
db_get_avg($table, $join="*", $column=null, $where=null)
~~~
刪除
~~~
db_del($table, $where)
db_delete($table, $where)
~~~
顯示所有表名
~~~
show_tables($table)
~~~
$where 條件:AND OR 查尋 https://medoo.in/api/where
~~~
"AND" => [
"OR" => [
"user_name" => "foo",
"email" => "foo@bar.com"
],
"password" => "12345"
]
~~~
SQL:
~~~
WHERE (user_name = 'foo' OR email = 'foo@bar.com') AND password = '12345'
~~~
$where 條件:更復雜的AND OR 查尋
~~~
[
"AND #Actually, this comment feature can be used on every AND and OR relativity condition" => [
"OR #the first condition" => [
"user_name" => "foo",
"email" => "foo@bar.com"
],
"OR #the second condition" => [
"user_name" => "bar",
"email" => "bar@foo.com"
]
]
]
~~~
SQL:
~~~
WHERE (
("user_name" = 'foo' OR "email" = 'foo@bar.com')
AND
("user_name" = 'bar' OR "email" = 'bar@foo.com')
)
~~~
**連接新的數據庫**
~~~
多數據庫時有用
$config['db_host'] = '127.0.0.1';
//數據庫名
$config['db_name'] = 'dbname';
//數據庫登錄用戶名
$config['db_user'] = 'root';
//數據庫登錄密碼
$config['db_pwd'] = '111111';
//數據庫端口號
$config['db_port'] = 3306;
$new_db = new_db($config = []);
~~~
用返回的`$new_db `進行操作。
如
~~~
$new_db->insert(table,data);
~~~
用法與Medoo一樣。