PHP code example of lipowei / db

1. Go to this page and download the library: Download lipowei/db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lipowei / db example snippets



cwei\Db\Db;  
/** 初始化 **/
$config = [//可将配置信息保存起来,以后无需配置就可直接使用:使用 Db::$configSave = true; + Db::crateSqlObj($config);下面有详细介绍 
           //主数据库,如果读写分离,主数据库服务器只写不读;如果读写不分离,将其他服务器的数据库链接信息加入host数组里面去就行了  
           'host' => ['localhost'],  
           'port' => ['3306'],  
           'database' => ['test'],  
           'username' => ['root'],  
           'password' => [''],  
           //utf8mb4相对utf8来说,utf8mb4支持emoji,但占的内存大一点
           'charset' => 'utf8mb4',  
           //是否显示SQL错误信息  
           'sqlDebug' => true,  
           //数据库部署方式:0单一服务器  1多个 mysql 服务器(2个或以上)  
           'deploy'  => 0,  
           //数据库读写是否分离,分布式有效  
           'rwSeparate' => false,  
           //在读写分离的环境下,是否开启一旦表有写操作,本次请求的后续操作涉及到该表的都使用写句柄进行操作,避免数据在读库尚未同步完成导致数据不一致
           'readSelectMaster' => false,
           //从数据库,从数据库服务器只读不写;注意:只有在读写分离才将链接信息写在下面  
           'slaveHost' => [],  
           'slavePort' => [],  
           'slaveDatabase' => [],  
           'slaveUsername' => [],  
           'slavePassword' => [],
         ];
//将配置信息传入类
Db::$config = $config;
/** 当使用这俩行代码时,只需要执行一次,配置信息保存以后无需再进行任何初始化
//重要:将配置信息保存到文件内,默认目录为 vendor 的同级目录 config 内的 tcweiDB.php;如果不是使用 composer 安装的该DB库则无法使用,因为该功能有依赖
Db::$configSave = true;
Db::crateSqlObj($config);
**/
/** 初始化结束 **/

//原生用法
//read查询使用 ? 占位进行预处理,可防止SQL注入;Db::read() 返回的是二维数组结果集或者无结果空数组
$row = Db::read("SELECT * FROM article WHERE id<? AND title like ?", [50,'%张三%']);
//write 写数据,更新、删除、插入等使用;执行返回的是受影响条数(新增条数、更新条数、删除条数);
$res = Db::write("INSERT INTO `article`(title,content,user_id) VALUES(?, ?, ?)", [$title, $content, $uid]);

//经典用法,全部自动使用预处理防SQL注入,自动加反引号防关键词冲突,这俩个和上面原生执行的返回结果一样
$row = Db::table('article')->where([ ['id', '<', 50], ['title', 'like', '%张三%'] ])->select();
$res = Db::table('article')->insert(['title'=>$title, 'content'=>$content, 'user_id'=>$uid]);

//获取单个数据,下面真实执行语句: SELECT `title` FROM `article` WHERE `id`=50 LIMIT 0,1  
$title = Db::table('article')->where('id', 50)->value('title');

//获取单行数据,返回的是一维数组 ['title'=>'标题', 'content'=>'文章内容']
$article = Db::table('article')->where('id', 50)->find('title,content');

//更新 id<50 的行进行更新,返回受影响条数
$res = Db::table('article')->where('id', '<', 50)->update(['title'=>'newTitle']);

//和上面执行一样,第2个参数为更新白名单,不存在白名单不更新
$res = Db::table('article')->where('id', '<', 50)->update($_POST, 'title');

//删除,默认条件下必须设置where才可以删除,否则报错;如果要强制无条件删除,设置 ->delete(true)
$res = Db::table('article')->where('id', '<', 50)->delete();


//事务执行
Db::transaction(function(){
                //写你的业务逻辑代码
               }, function($error){
                 //事务执行失败,在这里你选择是否结束程序执行。若要结束执行exit即可
                 //$error->getMessage(); $error是获取到的 \Exception $error
               });

->likeConcat('content', "'%',?,'%'", [$_POST['search']]);作用是防止需要模糊查询的内容里面包含 ? _ 这些特殊字符

//字符串模式例子
->order('time', 'desc');
//数组模式例子
DESC:
->order(['time'=>'desc']) 或者 ->order(['time'], 'DESC')
->order(['time'=>'desc', 'id'=>'desc']) 或者 ->order(['time', 'id'], 'DESC')
ASC:
->order(['time']) 效果: time ASC
->order(['time', 'id']) 效果: time ASC,id ASC

$res = Db::table('article')->noQuery()->where(['id', '<', 100])->order(['id'=>'desc', 'title'])->group('type')->limit(0, 10)->select();
echo $res;//SELECT * FROM `article` WHERE `id` < ? GROUP BY `type` ORDER BY `id` desc ,`title` ASC LIMIT 0,10 参数:[100]

insert后若需要获取自增ID,调用:
$id = Db::$insertId;

//获取指定的列值,返回一维数组
$res = Db::table('users')->where('id', '<', 10)->column('nickname');
//$res  ['张三', '李四', '王五'...]
$res = Db::table('users')->where('id', '<', 10)->column('nickname', 'id');
//$res  [uid=>'张三', uid=>'李四', uid=>'王五'...]