在Event.php 同级目录新建文件Config/Db.php

       <?php
       namespace Config;
       /**
        * mysql配置
        * @author walkor
        */
       class Db
       {
           /**
            * 数据库的一个实例配置,则使用时像下面这样使用
            * $user_array = Db::instance(‘user‘)->select(‘name,age‘)->from(‘users‘)->where(‘age>12‘)->query();
            * 等价于
            * $user_array = Db::instance(‘user‘)->query(‘SELECT `name`,`age` FROM `users` WHERE `age`>12‘);
            * @var array
            */
            public static $user = array(
                 ‘host‘    => ‘127.0.0.1‘,
                 ‘port‘    => 3306,
                 ‘user‘    => ‘your_user_name‘,
                 ‘password‘ => ‘your_password‘,
                 ‘dbname‘  => ‘user‘,
                 ‘charset‘    => ‘utf8‘,
                );
         }
       use \GatewayWorker\Lib\Db;
       $db1 = Db::instance(‘db1‘);
       $db2 = Db::instance(‘db2‘);

       // 获取所有数据
        $db1->select(‘ID,Sex‘)->from(‘Persons‘)->where(‘sex= :sex‘)->bindValues(array(‘sex‘=>‘M‘))->query();
       //等价于
       $db1->select(‘ID,Sex‘)->from(‘Persons‘)->where("sex= ‘F‘ ")->query();
       //等价于
       $db1->query("SELECT ID,Sex FROM `Persons` WHERE sex=‘M’");


       // 获取一行数据
       $db1->select(‘ID,Sex‘)->from(‘Persons‘)->where(‘sex= :sex‘)->bindValues(array(‘sex‘=>‘M‘))->row();
       //等价于
       $db1->select(‘ID,Sex‘)->from(‘Persons‘)->where("sex= ‘F‘ ")->row();
       //等价于
       $db1->row("SELECT ID,Sex FROM `Persons` WHERE sex=‘M’");


       // 获取一列数据
       $db1->select(‘ID‘)->from(‘Persons‘)->where(‘sex= :sex‘)->bindValues(array(‘sex‘=>‘M‘))->column();
        //等价于
       $db1->select(‘ID‘)->from(‘Persons‘)->where("sex= ‘F‘ ")->column();
       //等价于
       $db1->column("SELECT `ID` FROM `Persons` WHERE sex=‘M’");

        // 获取单个值
       $db1->select(‘ID,Sex‘)->from(‘Persons‘)->where(‘sex= :sex‘)->bindValues(array(‘sex‘=>‘M‘))->single();
       //等价于
      $db1->select(‘ID,Sex‘)->from(‘Persons‘)->where("sex= ‘F‘ ")->single();
      //等价于
      $db1->single("SELECT ID,Sex FROM `Persons` WHERE sex=‘M‘");

       // 复杂查询
       $db1->select(‘*‘)->from(‘table1‘)->innerJoin(‘table2‘,‘table1.uid = table2.uid‘)->where(‘age > :age‘)->groupBy(array(‘aid‘))->having(‘foo="foo"‘)->orderBy(array(‘did‘))->limit(10)->offset(20)->bindValues(array(‘age‘ => 13));
       // 等价于
        $db1->query(SELECT * FROM `table1` INNER JOIN `table2` ON `table1`.`uid` = `table2`.`uid` WHERE age > 13 GROUP BY aid HAVING foo="foo" ORDER BY did LIMIT 10 OFFSET 20“);

        // 插入
         $insert_id = $db1->insert(‘Persons‘)->cols(array(‘Firstname‘=>‘abc‘, ‘Lastname‘=>‘efg‘, ‘Sex‘=>‘M‘, ‘Age‘=>13))->query();
       //等价于
       $insert_id = $db1->query("INSERT INTO `Persons` ( `Firstname`,`Lastname`,`Sex`,`Age`) VALUES ( ‘abc‘, ‘efg‘, ‘M‘, 13)");

       // 更新
        $row_count = $db1->update(‘Persons‘)->cols(array(‘sex‘))->where(‘ID=1‘)->bindValue(‘sex‘, ‘F‘)->query();
       // 等价于
       $row_count = $db1->update(‘Persons‘)->cols(array(‘sex‘=>‘F‘))->where(‘ID=1‘)->query();
       // 等价于
       $row_count = $db1->query("UPDATE `Persons` SET `sex` = ‘F‘ WHERE ID=1");

       // 删除
        $row_count = $db1->delete(‘Persons‘)->where(‘ID=9‘)->query();
        // 等价于
        $row_count = $db1->query("DELETE FROM `Persons` WHERE ID=9");
Last modification:June 8th, 2020 at 09:13 pm
如果觉得我的文章对你有用,请随意赞赏