ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**使用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一样。