<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # ThinkPHP6 數據庫操作 * `PDO` 預處理 * 要使用Db類必須使用門面方式( `think\facade\Db` )調用 * 數據庫操作統一入口: `Db::` --- ### 一、執行原生 `MySql` #### 1、`query` 方法用于執行 `MySql` 查詢操作 ```php 示例: namespace app\index\controller; use app\BaseController; use think\facade\Db; class Index extends BaseController{ public function index(){ $query = Db::query("select * from user where status=1"); print_r($query); } } ``` #### 2、`execute` 方法用戶執行 `MySql` 新增和修改操作 ```php 示例: namespace app\index\controller; use app\BaseController; use think\facade\Db; class Index extends BaseController{ public function index(){ $execute = Db::execute("insert into user set `phone`='13211111111',`u_name`='一燈大師' "); print_r($execute); $execute = Db::execute("update user set `u_name`='武三通' where `uid`=6 "); print_r($execute); } } ``` --- ### 二、查詢 > 1、單條數據查詢 `find` * `find` 方法查詢結果不存在,返回 `null`,否則返回結果數組 ```php 示例: namespace app\index\controller; use app\BaseController; use think\facade\Db; class Index extends BaseController{ public function index(){ $find = Db::table('user')->where('uid',1)->find(); print_r($find); } } ``` > 2、多條數據查詢 `select` * `select` 方法查詢結果是一個二維數組,如果結果不存在,返回空數組 ```php 示例: $select = Db::table('user')->where('status',1)->select(); print_r($select); ``` > 3、查詢某個字段的值 `value` * `value` 方法查詢結果不存在,返回 null ```php 示例: $value = Db::table('user')->where('uid',1)->value('u_name'); print_r($value); ``` > 4、查詢某一列的值 `column` * `column` 方法查詢結果不存在,返回空數組 ```php 示例: $column = Db::table('user')->where('uid',1)->column('u_name'); print_r($column); $column = Db::table('user')->where('uid',1)->column('u_name','uid'); print_r($column); ``` --- ### 三、添加 > 1、添加一條數據 `insert` * `insert` 方法添加數據成功返回添加成功的條數,通常情況返回 1 ```php 示例: # 添加數據 $data = ['phone'=>'13355555555','u_name'=>'穆念慈','sex'=>2]; $insert = Db::table('user')->insert($data); print_r($insert); ``` > 2、添加、修改一條數據 `save` * `save` 方法統一寫入數據,自動判斷是新增還是更新數據(以寫入數據中是否存在主鍵數據為依據)。 ```php 示例: # 添加數據 $data = ['phone'=>'13355555555','u_name'=>'黃藥師','sex'=>1]; $save = Db::table('user')->save($data); print_r($save); # 修改數據 $data = ['uid'=>6,'phone'=>'13366666666','u_name'=>'黃藥師','sex'=>1]; $save = Db::table('user')->save($data); print_r($save); ``` > 3、添加一條數據 `insertGetId` * `insertGetId` 方法添加數據成功返回添加數據的自增主鍵 ```php 示例: # 添加數據,返回增加的數據主鍵ID $data = ['phone'=>'13377777777','u_name'=>'洪七公','sex'=>1]; $insert = Db::table('user')->insertGetId($data); print_r($insert); ``` > 4、添加多條數據 `insertAll` * `insertAll` 方法添加數據成功返回添加成功的條數 ```php 示例: # 增加三條數據 $data = [ ['phone'=>'13388888888','u_name'=>'段智興','sex'=>1], ['phone'=>'13399999999','u_name'=>'周伯通','sex'=>1], ['phone'=>'13300000000','u_name'=>'瑛姑','sex'=>2], ['phone'=>'13400000000','u_name'=>'歐陽鋒','sex'=>1], ['phone'=>'13411111111','u_name'=>'王重陽','sex'=>1] ]; $insert = Db::table('user')->insertAll($data); print_r($insert); ``` --- ### 四、修改 > 1、修改數據 `update` * `update` 方法返回影響數據的條數,沒修改任何數據返回 0 ```php 示例: # 修改數據 $data = ['phone'=>'13422222222','u_name'=>'一燈大師']; $update = Db::table('user')->where('uid',9)->update($data); print_r($update); ``` > 2、添加、修改一條數據 `save` * `save` 方法統一寫入數據,自動判斷是新增還是更新數據(以寫入數據中是否存在主鍵數據為依據)。 ```php 示例: # 修改數據 $data = ['phone'=>'13433333333','u_name'=>'中神通周伯通']; $save = Db::table('user')->where('uid',10)->save($data); print_r($save); ``` > 3、自增 `inc` * `inc` 方法自增一個字段的值 ```php 示例: # 字段的值增加1 $inc = Db::table('user')->where('uid',6)->inc('age')->update(); print_r($inc); # 字段的值增加5 $inc = Db::table('user')->where('uid',7)->inc('age',5)->update(); print_r($inc); ``` > 4、自減 `dec` * `dec` 方法自減一個字段的值 ```php 示例: # 字段的值減去1 $dec = Db::table('user')->where('uid',8)->dec('age')->update(); print_r($dec); # 字段的值減去5 $dec = Db::table('user')->where('uid',9)->dec('age',5)->update(); print_r($dec); ``` --- ### 五、刪除 > 1、刪除數據 `delete` * `delete` 方法返回影響數據的條數,沒有刪除返回 0 ```php 示例: # 根據條件刪除數據 $delete = Db::table('user')->where('uid',1)->delete(); print_r($delete); # 刪除主鍵為2的數據 $delete = Db::table('user')->delete(2); print_r($delete); # 刪除整表數據 $delete = Db::name('user')->delete(true); print_r($delete); ``` > 2、軟刪除 `useSoftDelete` * 業務數據不建議真實刪除數據,TP系統提供了軟刪除機制 ```php 示例: # 軟刪除 $delete = Db::name('user')->useSoftDelete('status',2)->delete(); print_r($delete); ``` --- ### 備注 > 增刪查改是常規操作 > 下面的教程 在增刪查改基礎上 增加更多功能 --- ### 六、查詢表達式 **表達式**|**含義**|**快捷查詢方法** ---|---|---|--- =|等于| <>|不等于| >|大于| >=|大于等于| <|小于| <=|小于等于| [NOT] LIKE|模糊查詢|`whereLike/whereNotLike` [NOT] BETWEEN|區間查詢|`whereBetween/whereNotBetween` [NOT] IN|IN 查詢|`whereIn/whereNotIn` [NOT] NULL|查詢字段是否為NULL|`whereNull/whereNotNull` * > < >= <= 是給數字使用 * = <> 可以給字符串使用,也可以用在數字 > 1、表達式 * 表達式是SQL語句的條件 * 表達式不分大小寫 * 表達式寫在where里 ```php 示例: # 等于(=) $select = Db::table('user')->where('uid','=','1')->select(); print_r($select); # 不等于(<>) $select = Db::table('user')->where('uid','<>','2')->select(); print_r($select); # 大于(>) $select = Db::name('user')->where('uid','>','3')->select(); print_r($select); # 大于等于(>=) $select = Db::name('user')->where('uid','>=','4')->select(); print_r($select); # 小于(<) $select = Db::name('user')->where('uid','<','5')->select(); print_r($select); # 小于等于(<=) $select = Db::name('user')->where('uid','<=','6')->select(); print_r($select); # LIKE $select = Db::name('user')->where('u_name','like','歐陽%')->select(); print_r($select); $select = Db::name('user')->whereLike('u_name','歐陽%')->select(); print_r($select); # NOT LIKE $select = Db::name('user')->where('u_name','not like','歐陽%')->select(); print_r($select); $select = Db::name('user')->whereNotLike('u_name','歐陽%')->select(); print_r($select); # BETWEEN $select = Db::name('user')->where('uid','between','4,7')->select(); print_r($select); $select = Db::name('user')->whereBetween('uid','4,7')->select(); print_r($select); # NOT BETWEEN $select = Db::name('user')->where('uid','not between','4,7')->select(); print_r($select); $select = Db::name('user')->whereNotBetween('uid','4,7')->select(); print_r($select); # IN $select = Db::name('user')->where('uid','in','4,7')->select(); print_r($select); $select = Db::name('user')->whereIn('uid','4,7')->select(); print_r($select); # NOT IN $select = Db::name('user')->where('uid','not in','4,7,10')->select(); print_r($select); $select = Db::name('user')->whereNotIn('uid','4,7')->select(); print_r($select); # NULL $select = Db::name('user')->where('u_name',null)->select(); $select = Db::name('user')->where('u_name','null')->select(); $select = Db::name('user')->where('u_name','=','null')->select(); // 字符串不為空 print_r($select); $select = Db::name('user')->whereNull('u_name')->select(); print_r($select); # NOT NULL $select = Db::name('user')->where('u_name','not null')->select(); print_r($select); $select = Db::name('user')->whereNotNull('u_name')->select(); print_r($select); ``` --- ### 七、鏈式查詢 **連貫操作**|**作用**|**支持的參數類型** ---|---|---|--- where|用于AND查詢|字符串、數組和對象 table|用于定義要操作的數據表名稱|字符串和數組 alias|用于給當前數據表定義別名|字符串 field|用于定義要查詢的字段(支持字段排除)|字符串和數組 order|用于對結果排序|字符串和數組 limit|用于限制查詢結果數量|字符串和數字 page|用于查詢分頁(內部會轉換成limit)|字符串和數字 join|用于對查詢的join支持|字符串和數組 union|用于對查詢的union支持|字符串、數組和對象 distinct|用于查詢的distinct支持|布爾值 > 1、`where` 方法主要用于數據庫查詢條件 * 普通查詢、表達式查詢、快捷查詢、區間查詢、組合查詢 ```php 示例: # 字符串查詢: $select = Db::table('user')->where('status=1')->select(); print_r($select); # 表達式查詢:官方推薦使用的查詢方式 $select = Db::table('user') ->where('uid','>',1) ->where('u_name','黃蓉') ->select(); print_r($select); # 數組條件:數組條件,會有更復雜的查詢 $select = Db::table('user')->where([ 'u_name' => '楊康', 'status'=> 1 ])->select(); print_r($select); ``` > 2、`table` 方法主要用于指定操作的數據表 ```php 示例: # 單表查詢 $select = Db::table('user')->where('status=1')->select(); print_r($select); # 多表查詢 $select = Db::table('user u,orders o')->where('u.status=1')->select(); print_r($select); # 數組多表查詢 $select = Db::table([ 'user' => 'u', 'orders' => 'o' ])->where('u.status=1')->select(); print_r($select); ``` > 3、`alias` 方法主要用于設置當前數據表的別名 ```php 示例: $select = Db::table('user')->alias('u')->where('status=1')->select(); print_r($select); ``` > 4、`field` 方法主要作用是標識要返回或者操作的字段,可以用于查詢和寫入操作 * `withoutField` 字段排除 * `fieldRaw` 使用mysql函數 ```php 示例: # 返回三個字段 $select = Db::table('user')->field('uid,phone,u_name')->select(); print_r($select); # 返回字段改名 $select = Db::table('user')->field('uid,phone,u_name as n')->select(); print_r($select); # 數組 $select = Db::table('user')->field(['uid','phone','u_name'])->select(); print_r($select); # 字段排除 $select = Db::table('user')->withoutField('uid,status')->select(); print_r($select); # 使用mysql函數 $select = Db::table('user')->fieldRaw('MAX(age)')->select(); print_r($select); ``` > 5、`order` 方法用于對操作的結果排序或者優先級限制 * 如果沒有指定 `desc` 或者 `asc` 排序規則的話,默認為 `asc` ```php 示例: # 年齡倒序 $select = Db::table('user')->field('uid,phone,u_name,age')->where('status=1')->order('age','desc')->select(); print_r($select); # 排序數組 $select = Db::table('user')->field('uid,phone,u_name,age')->where('status=1')->order([ 'age' => 'desc' ])->select(); print_r($select); ``` > 6、`limit` 方法主要用于指定查詢和操作的數量 ```php 示例: # 查詢3條數據 $select = Db::table('user')->field('uid,phone,u_name,age')->where('status=1')->order('age','desc')->limit(3)->select(); print_r($select); # 從第3條開始查詢出10條數據 $select = Db::table('user')->field('uid,phone,u_name,age')->where('status=1')->order('age','desc')->limit(3,10)->select(); print_r($select); ``` > 7、`page` 方法主要用于分頁查詢 ```php 示例: # 從第0條開始查詢出10條數據 $select = Db::table('user')->field('uid,phone,u_name,age')->where('status=1')->order('age','desc')->page(0,10)->select(); print_r($select); # page 組合 limit $select = Db::table('user')->field('uid,phone,u_name,age')->where('status=1')->order('age','desc')->limit(5)->page(3)->select(); print_r($select); ``` > 8、`join` 方法用于根據兩個或多個表中的列之間的關系,從這些表中查詢數據。join通常有下面幾種類型,不同類型的join操作會影響返回的數據結果 **鏈式**|**說明**|**示例** ---|---|--- join|內連接,等同于 JOIN(默認的JOIN類型),如果表中有至少一個匹配,則返回行|`join ( mixed join [, mixed $condition = null [, string $type = 'INNER']] )` leftJoin|左連接,即使右表中沒有匹配,也從左表返回所有的行|`leftJoin ( mixed join [, mixed $condition = null ] )` rightJoin|右連接,即使左表中沒有匹配,也從右表返回所有的行|`rightJoin ( mixed join [, mixed $condition = null ] )` fullJoin|只要其中一個表中存在匹配,就返回行|`fullJoin ( mixed join [, mixed $condition = null ] )` **參數**|**說明** ---|--- join|要關聯的(完整)表名以及別名 condition|關聯條件。可以為字符串或數組, 為數組時每一個元素都是一個關聯條件 type|關聯類型。可以為:`INNER`、`LEFT`、`RIGHT`、`FULL`,不區分大小寫,默認為`INNER` ```php 示例: # 內連接 $select = Db::table('user u')->join('orders o','o.uid = u.uid')->select(); print_r($select); # 左連接 $select = Db::table('user u')->leftJoin('orders o','o.uid = u.uid')->select(); print_r($select); # 右連接 $select = Db::table('user u')->rightJoin('orders o','o.uid = u.uid')->select(); print_r($select); ``` * 備:fullJoin操作比較麻煩,使用UNION代替 > 9、`union` 操作用于合并兩個或多個 SELECT 語句的結果集 * `union` 內部的 `SELECT` 語句必須擁有相同數量的列。列也必須擁有相似的數據類型。同時,每條 `SELECT` 語句中的列的順序必須相同 ```php 示例: $select = Db::field('u_name')->table('user')->union('SELECT `u_name` FROM user1')->select(); print_r($select); ``` > 10、`distinct` 方法用于返回唯一不同的值 ```php 示例: $select = Db::table('user')->distinct(true)->select(); print_r($select); ``` --- ### 八、聚合查詢 **方法**|**功能** ---|--- count|統計數量,參數是要統計的字段名(可選) max|獲取最大值,參數是要統計的字段名(必須) min|獲取最小值,參數是要統計的字段名(必須) avg|獲取平均值,參數是要統計的字段名(必須) sum|獲取總數,參數是要統計的字段名(必須) ```php 示例: // 統計數量,參數是要統計的字段名(可選) $select = Db::table('user')->count(); print_r($select); // 獲取最大值,參數是要統計的字段名(必須) $select = Db::table('user')->max('age'); print_r($select); // 獲取最小值,參數是要統計的字段名(必須) $select = Db::table('user')->min('age'); print_r($select); // 獲取平均值,參數是要統計的字段名(必須) $select = Db::table('user')->avg('age'); print_r($select); // 獲取總數,參數是要統計的字段名(必須) $select = Db::table('user')->sum('age'); print_r($select); ``` --- ### 九、分頁查詢 * `paginate` 分頁方法 * 注:`thinkphp` 分頁樣式是用 `bootstrap` ```php php示例: $select = Db::table('user')->paginate(10); View::assign([ 'select' => '$select' ]); return View::fetch(); ``` ```html html示例: {volist name='select' id='vo'} <div>{$vo.u_name}</div> {/volist} {$list|raw} ``` * 單獨獲取分頁 ```php php示例: $select = Db::table('user')->paginate(10); $page = $select->render(); View::assign([ 'select' => $select, 'page' => $page, ]); return View::fetch(); ``` ```html html示例: {volist name='select' id='vo'} <div>{$vo.u_name}</div> {/volist} {$page|raw} ``` * 獲取總頁數 ```php php示例: $select = Db::table('user')->paginate(10); $count = $select->total(); View::assign([ 'select' => $select, 'count' => $count, ]); return View::fetch(); ``` ```html html示例: {volist name='select' id='vo'} <div>{$vo.u_name}</div> {/volist} {$count} ``` ```css bootstrap示例: .pagination { display: inline-block; padding-left: 0; margin: 20px 0; border-radius: 4px; } .pagination > li { display: inline; } .pagination > li > a, .pagination > li > span { position: relative; float: left; padding: 6px 12px; margin-left: -1px; line-height: 1.42857143; color: #337ab7; text-decoration: none; background-color: #fff; border: 1px solid #ddd; } .pagination > li:first-child > a, .pagination > li:first-child > span { margin-left: 0; border-top-left-radius: 4px; border-bottom-left-radius: 4px; } .pagination > li:last-child > a, .pagination > li:last-child > span { border-top-right-radius: 4px; border-bottom-right-radius: 4px; } .pagination > li > a:hover, .pagination > li > span:hover, .pagination > li > a:focus, .pagination > li > span:focus { z-index: 2; color: #23527c; background-color: #eee; border-color: #ddd; } .pagination > .active > a, .pagination > .active > span, .pagination > .active > a:hover, .pagination > .active > span:hover, .pagination > .active > a:focus, .pagination > .active > span:focus { z-index: 3; color: #fff; cursor: default; background-color: #337ab7; border-color: #337ab7; } .pagination > .disabled > span, .pagination > .disabled > span:hover, .pagination > .disabled > span:focus, .pagination > .disabled > a, .pagination > .disabled > a:hover, .pagination > .disabled > a:focus { color: #777; cursor: not-allowed; background-color: #fff; border-color: #ddd; } ``` --- ### 十、`SQL` 調試 * `getLastSql` 輸出上次執行的sql語句 ```php // 獲取總數,參數是要統計的字段名(必須) $select = Db::table('user')->sum('age'); print_r($select); echo Db::getLastSql(); ``` * `getLastSql` 方法只能獲取最后執行的 `SQL` 記錄 ```php // 獲取總數,參數是要統計的字段名(必須) $select = Db::table('user')->fetchSql()->sum('age'); echo $select; ``` * `fetchSql` 方法直接返回當前的 `SQL` 而不執行 --- ### 十一、動態配置數據庫 * `connect` 方法動態配置數據庫連接信息 ```php # 字符串配置 # 格式:數據庫類型://用戶名:密碼@數據庫地址:數據庫端口/數據庫名#字符集 $data = Db::connect('mysql://root:root@127.0.0.1:3306/huangrong#utf8')->table('admin')->find(); print_r($data); # 數組配置 $data = Db::connect([ // 數據庫類型 'type' => 'mysql', // 數據庫連接DSN配置 'dsn' => '', // 服務器地址 'hostname' => '127.0.0.1', // 數據庫名 'database' => 'huangrong', // 數據庫用戶名 'username' => 'root', // 數據庫密碼 'password' => 'root', // 數據庫連接端口 'hostport' => '3306', // 數據庫連接參數 'params' => [], // 數據庫編碼默認采用utf8 'charset' => 'utf8', // 數據庫表前綴 'prefix' => '', ])->table('admin')->find(); print_r($data); ``` > connect 方法必須在查詢的最開始調用,而且必須緊跟著調用查詢方法,否則可能會導致部分查詢失效或者依然使用默認的數據庫連接。 * `config` 目錄多數據庫配置 ```php return [ // 數據庫類型 'type' => 'mysql', // 服務器地址 'hostname' => '127.0.0.1', // 數據庫名 'database' => 'thinkphp', // 數據庫用戶名 'username' => 'root', // 數據庫密碼 'password' => '', // 數據庫連接端口 'hostport' => '', // 數據庫連接參數 'params' => [], // 數據庫編碼默認采用utf8 'charset' => 'utf8', // 數據庫表前綴 'prefix' => '', //數據庫配置1 'db_config1' => [ // 數據庫類型 'type' => 'mysql', // 服務器地址 'hostname' => '127.0.0.1', // 數據庫名 'database' => 'huangrong', // 數據庫用戶名 'username' => 'root', // 數據庫密碼 'password' => 'root', // 數據庫編碼默認采用utf8 'charset' => 'utf8', // 數據庫表前綴 'prefix' => '', ], //數據庫配置2 'db_config2' => 'mysql://root:root@127.0.0.1:3306/huangrong#utf8', ]; ``` ```php Db::connect('db_config1')->table('user')->find(); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看