`
xieye
  • 浏览: 802422 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

php的db类库Eloquent单独使用系列(1)

    博客分类:
  • PHP
阅读更多
我的Eloquent单独使用系列文章
php的db类库Eloquent单独使用系列(1)
php的db类库Eloquent单独使用系列(2) - 分页
php的db类库Eloquent单独使用系列(3) - sql日志
php的db类库Eloquent单独使用系列(4)- 事件监听
php的db类库Eloquent单独使用系列(5)- 模型转数组
php的db类库Eloquent单独使用系列(6)- 一对一关联
php的db类库Eloquent单独使用系列(7)- 一对多关联
php的db类库Eloquent单独使用系列(8)- 多对多关联
php的db类库Eloquent单独使用系列(9)- 多对多关联 - 表关联自身
php的db类库Eloquent单独使用系列(10)- 多对多关联 - 远程一对多
php的db类库Eloquent单独使用系列(11)- 多对多关联 - 添加模型属性
php的db类库Eloquent单独使用系列(12)- 结果集模型转数组 - 2

elquent查询集锦:这是一个外链,作者chajinglong
Laravel SQL 查询语句集锦

composer:

"illuminate/database":"5.4.27",
"illuminate/events":"5.4.27" 这个可不加载,但使用它提供的模型类就要加载。

请注意,这个版本需要php5.6支持。

表结构如下:
CREATE TABLE `test_databases` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `db_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '库名',
  `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '测试用户id',
  `created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  `updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `test_databases_db_name_user_id_unique` (`db_name`,`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


insert into test_databases values(1,'哈哈',1,11,22);
insert into test_databases values(2,'嗯嗯',2,33,44);


示例:
<?php

namespace app\control;
use \Illuminate\Database\Capsule\Manager as Capsule;
// use \Illuminate\Events\Dispatcher;
// use \Illuminate\Container\Container;

class Ill {
   
 public function index( $req, $res, $args) {
       $capsule = new Capsule;
       $capsule->addConnection([
           'driver'    => 'mysql',
           'host'      => '127.0.0.1',
           'database'  => 'test',
           'username'  => 'root',
           'password'  => 'root',
           'charset'   => 'utf8',
           'collation' => 'utf8_unicode_ci',
           'prefix'    => '',
       ]);
       $capsule->setAsGlobal();
       $conn =$capsule; 
       echo "<h1>select(获取全部), (获取行)没有, fetchColumn没有</h1>";
       $users = $conn::select('SELECT * FROM test_databases limit 2');
       var_dump($users);
       echo "<hr>";
       //
       echo "插入示例<br>";
       $conn::insert('insert into test_databases (user_id, db_name) values (?, ?)', [time(), '汽车']);
     //  echo "插入id为:". $conn::insertGetId();
       echo "修改示例<br>";
      $result = $conn::update('update test_databases set user_id=user_id+'.mt_rand(10000,99999).
              ' where db_name=?', ['汽车']);
       echo "更新影响的行数:". $result."<br>";
       $result = $conn::delete('delete from test_databases where db_name=? limit 1', ['汽车']);
       echo "删除影响的行数:". $result."<br>";
       
       // 这里执行表结构定义,加减字段等语句
     //  DB::statement('drop table test_databases');
       echo "<h2>查询构造器使用</h2>";
       //获取行
       echo "<h5>获取单行</h5>";
       $user = $conn::table('test_databases')->where('id', 1)->first();
       
       echo $user->db_name."<br>";
       echo "<h5>获取单个值</h5>";
       echo $conn::table('test_databases')->where('id', 1)->value('db_name') . "<br>";
       echo "<h5>获取多行</h5>";
       $user = $conn::table('test_databases')->where('id','<', 3)->get();
       var_dump($user);echo "<br>";
       echo $user[0]->db_name."<br>";
       
       echo "<h5>获取一列数据</h5>";
       $titles = $conn::table('test_databases')->where('id','<', 3)->pluck("db_name");
       echo $titles[0];
       
       echo "<h5>插入数据</h5>";
       $id = $conn::table('test_databases')->insertGetId(
               ['db_name' => mt_rand(1000,10000), 'user_id' => mt_rand(1000,10000)]
       );
       echo "<h5>插入id:{$id}</h5>";
       
       echo "<h5>更新数据示例</h5>";
       $result = $conn::table('test_databases')
         ->where('id', 1)
         ->update(['user_id' => mt_rand(10,10000)]);
       echo "更新行数". $result;
         
       
       echo "<h5>删除数据示例</h5>";
       $result = $conn::table('test_databases')
         ->where('id', $id)
         ->delete();
       echo "删除行数". $result;
       return $res;
   }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics