# 数据库

数据库操作是基于 Medoo Version: 2.1.8 进行改造再封装。

https://medoo.in/

在使用前需要对Medoo 数据库类有足够的了解,否则有可能无法理解参数传递等问题。

# 数据防止更新出错

1.使用db_raw ,推荐使用此方法

db_update('plugin',[
	'level'=>db_raw('level-1')
],['id'=>9]); 

2.使用 db_for_update

db_action(function(){
  db_for_update("plugin",9);  
  $level = db_get_one("plugin",'level',['id'=>9]);
  db_update('plugin',['level'=>$level-1],['id'=>9]); 
});

# FIND_IN_SET

$where = [
    'type[FIND_IN_SET]'=>10
];

# RAW

$where = [
	"aa[RAW]" => db_raw("FIND_IN_SET('error',type)")
];

其中aa是无关紧要的的key,主要用于防止重复,生成的SQL是db_raw中的SQL

# 输出SQL

db()->last()

# SQL查寻

db_query($sql, $raw = null)

# 取最小值

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_allow($table, $data)

# 查寻多条记录

$data = db()->select("account", [
	"user_name",
	"email"
	], [
	"user_id[>]" => 100
]);

db()->select 等价于 db_get()

# 查一条数据

$res = db_get_one("drop_account","*",['id'=>$id]);  

# 更新记录

db_update("drop_account",[
          'status'=>$status,
          'updated_at'=>now(),
          'admin_user_id'=>get_admin_id(),
],['id'=>$id]); 

# 删除记录

db_del($table, $where)

# 动作事件

do_action("db_insert.$table.before", $data);	添加数据前	
do_action("db_insert.$table.after", $action_data);	添加数据后 db_insert时触发	$action_data = [];
$action_data['id'] = $id;
$action_data['data'] = $data;
do_action("db_update.$table.before", $data);	更新数据前 db_insert时触发	 
do_action("db_update.$table.after", $action_data);	更新数据后  db_update时触发	$action_data = [];
$action_data['where'] = $where;
$action_data['data'] = $data;
do_action("db_get_one.$table", $one);	查寻一条记录 db_get_one时触发	
do_action("db_get.$table", $all);	查寻多条记录 db_get时触发	
do_action("db_insert.$table.del", $where);	删除记录 db_del($table, $where)时触发	
do_action("db_query", $all);	db_query时触发	

# 事务

db_action(function(){
   //如果有return false;就会回滚
});

# 分页

db_pager("drop_account","*",$where); 

# 连接其他数据库

new_db([
    'db_host'=>$config['db_host'],
    'db_name'=>"数据库名",
    'db_user'=>$config['db_user'],
    'db_pwd'=>$config['db_pwd'],
    'db_port'=>$config['db_port'],
],$active_db);
db_active($active_db);  

# 激活默认数据库连接

db_active_default()

# 激活读数据库连接

db_active_read()

查看主从配置 (opens new window)

更多可查看源码 (opens new window)

# 完整演示

include __DIR__.'/../../../../app.php'; 
//注销帐号.管理
access('drop_account.admin');

$wq = g("wq");
$where = [
  'ORDER'=>[
    'id'=>'DESC'
  ], 
];  
if($wq){
  $where["phone[~]"] = $wq;
}   
$where['status'] = $input['status']?:0; 
$data = db_pager("drop_account","*",$where); 
$list = [];
foreach($data['data'] as &$v){   
} 
json($data);

分页table

<?php   
/*
	Copyright (c) 2021-2050 FatPlug, All rights reserved.
	This file is part of the FatPlug Framework (http://fatplug.cn).
	This is not free software.
	you can redistribute it and/or modify it under the
	terms of the License after purchased commercial license. 
	mail: sunkangchina@163.com
	web: http://fatplug.cn
*/
global $config; 
$config['title'] = "注销帐号";
include __DIR__.'/../../app.php';      
admin_header(); 
check_admin_login();
$vue = new Vue;  
$vue->opt = [
    'is_page'  => true,
    'is_reset' => false,
    'is_add'   => false,
    'is_edit'  => false, 
];
$vue->page_url = "/plugins/drop_account/api/admin/drop_account.php";  
$vue->data("activeName","0"); 
$vue->data("height",""); 
$vue->created(["start()"]);
$vue->method("start()","js: 
	this.fn();
	if (window.addEventListener) {
	    window.addEventListener('resize', function() {
	      app.fn();
	    });
	} else if (window.attachEvent) {
	    window.attachEvent('onresize', function() {
	      app.fn();
	    });
	} 
");
$vue->method("fn()","js:
this.height = window.innerHeight-180;
");


$vue->data("ch","js:[]");
$vue->method("handleClick()","js: 
	this.where.status = this.activeName; 
	this.where.page = 1;
	this.load();
");

$vue->method("confirm(row,status)","js: 
	let id = row.id;
	let txt = '确认拒绝该操作吗?';
	let by = '拒绝后用户还可以再次发起注销申请,<br>此操作不会注销用户,可放心操作';
	let cf = '确认拒绝';
	if(status==1){
		txt = '确认注销该帐号吗?';
		by = '<span style=\"color:red;\">该操作次注销用户所有相关数据包含但不限:<br>用户基础信息(头像、昵称、手机号等)、订单信息、会员信息等。<br>请确认好再操作,该操作不可逆!!!</span>';
		cf = '确认注销帐号';
	}
	layer.confirm(txt, {
	  title:'操作提醒',
	  content:'帐号:'+row.phone+'<br>'+by,
	  btn: [cf,'取消'] //按钮
	}, function(){
		layer.load();
	  	ajax('/plugins/drop_account/api/admin/drop_account_confirm.php',{
	  		id:id,
	  		status:status
	  	},function(){
	  		layer.closeAll();
	  		_this.load();
	  		_this.\$message({
	  			type:res.type,
	  			message:res.msg, 
	  		});
	  	});
	}, function(){
	   layer.closeAll();
	});
	 
");


 

$js  = $vue->run(); 
?>
 
<div id="app"  v-cloak>   
	  <div class="main_body"> 
		  <el-tabs v-model="activeName" @tab-click="handleClick" >
		    <el-tab-pane label="待审核" name="0"> 
		    	<?php include __DIR__.'/_table.php';?>
		    </el-tab-pane>
		    <el-tab-pane label="已注销" name="1" >
		    	 <?php include __DIR__.'/_table.php';?>
		    </el-tab-pane> 
		    <el-tab-pane label="已拒绝" name="-1" >
		    	 <?php include __DIR__.'/_table.php';?>
		    </el-tab-pane> 
		  </el-tabs>  
	      <p>
	    	<el-pagination background class="mt10"  
		      @size-change="page_size_change"
		      @current-change="page_change"
		      :current-page="page"
		      :page-sizes="[20,50,100,500,1000]"
		      :page_size="where.pre_page"
		      layout="total, sizes, prev, pager, next, jumper"
		      :total="total">
		    </el-pagination> 
		</p>
    </div> 
     


</div>

<script type="text/javascript">
	<?= $js?> 
</script>
<?php 
admin_footer();
?>

_table.php 内容

<?php if(!defined("PATH")){exit();}?>
<el-table  border    :height="height"
  :data="lists"
  style="width: 100%"> 
  <el-table-column 
    prop="phone"
    label="帐号"
    width="">
  </el-table-column>  
  <el-table-column 
    prop="user_id"
    label="用户ID"
    width="100">
  </el-table-column> 
  <el-table-column 
    prop="created_at"
    label="申请时间"
    width="180">
  </el-table-column> 
  <el-table-column
    prop="updated_at"
    label="审核时间"
    width="180">
  </el-table-column> 
  <el-table-column v-if="activeName == 0"
    prop="comment"
    label="操作"
    width="180">
    <template slot-scope="scope"> 
      <div v-if="scope.row.status==0">
         <a href="javascript:void(0);"  @click="confirm(scope.row,1)" class="link hand"  >确认注销</a>
         <a href="javascript:void(0);"  @click="confirm(scope.row,-1)" class="link hand ml20">拒绝注销</a>
      </div> 
    </template>  
  </el-table-column>   
</el-table>  

# 获取数据库结构

数组

database_tables()

# 非当前数据库结构

database_tables($database_name)

# markdown格式

在config.ini.php中配置的数据库链接信息必须同时拥有$database_name权限 markdown格式

pr(database_tables(null,true));

返回结果:

upload 上传文件

| 字段  |  类型 | 备注|
| ------------ | ------------ |------------ |
|  id |  int(11) |主键|
|  url |  varchar(255) |URL|
|  hash |  varchar(255) |唯一值|
|  user_id |  int(11) |用户ID|
|  mime |  varchar(255) |类型|
|  size |  decimal(20,2) |大小|
|  ext |  varchar(10) |后缀|
|  created_at |  datetime |创建时间|
|  name |  varchar(255) |文件名|

# markdown转html

echo Parsedown::instance()
  ->setMarkupEscaped(true)
  ->text(database_tables(null,true)); 

# 数据表及字段

$all = database_tables();
$table = [];
foreach($all as $v){
  $table_name = $v['Name'];
  $table[$table_name] = get_table_fields($table_name);
}
pr($table);

返回

Array
(
     
    [config] => Array
        (
            [id] => id
            [title] => title
            [body] => body
        )
)