批改状态:合格
老师批语:只要是web开发, 数据库都很重要
first(): 查询单条get(): 查询多条all(): 输出格式转换,数组select(): 选择要查询字段as: 取别名where(): 条件查询, 允许有多个(and)orWhere(): 条件查询, 允许有多个(or)like 模糊查询tosql(): 查看生 sql 语句where in(): 多条件join(): 连表查询avg('pc'): 平均sum('pc'): 求和min('pc'): 最小max('pc'): 最大count(): 表记录数insert(): 参数是 array,支持二维insertGetId: 插入并返回 idupdate(): 更新, 参数是 arraydelete(): 删除aravel 操作数据库方法演示:
// 数据库查询(链式)// first() 单条记录public function finds(){echo '<pre>';// first() 单条记录$res = DB::table('article')->where('id', 2)->first();print_r($res);}// 数据库查询(链式)// 查询多条 get()// all() : 输出格式转换// select(): 选择要查询字段// as: 取别名// where(): 条件查询, 允许有多个(and)// orWhere(): 条件查询, 允许有多个(or)public function list(){echo '<pre>';$res = DB::table('article')->select('cate_id as cid', 'title as 标题')->where('cate_id', '=', 2)->get()->all();print_r($res);}// 数据库查询(链式)// 查询多条 get()// like 查询// where():// tosql (): 查看生sql语句public function likes(){echo '<pre>';// $res = DB::table('article')->where('cate_id', '=', '2')// ->where('id', '>', '2')->get()->all();$res = DB::table('article')->where('cate_id', '=', '2')->orWhere('cate_id', '=', '8')->tosql();//->get()->all();print_r($res);}// 数据库查询(链式)// where in()public function whereIn(){echo '<pre>';$res = DB::table('article')->whereIn('id', [1,2,5])->get()->all();print_r($res);}// 数据库查询(链式)// join() 连表查询public function joins(){echo '<pre>';$res = DB::table('article')->join('users', 'users.id', '=', 'article.uid')->select('article.title', 'users.name')->get()->all();print_r($res);}// 数据库查询(链式)// 聚合函数public function pvs(){echo '<pre>';// $res = DB::table('article')->avg('pc');// $res = DB::table('article')->sum('pc');// $res = DB::table('article')->min('pc');// $res = DB::table('article')->max('pc');$res = DB::table('article')->count();print_r($res);}// 数据库增加(链式)// insert():参数是array,支持二维public function insert2(){$item = ['uid'=>2, 'cate_id'=>6, 'title'=>'如何解决php错误不提示 ,空白页面的问题', 'pc'=>200];$item1 = ['uid'=>2, 'cate_id'=>1, 'title'=>'PHP 转 Go 还是转 Java', 'pc'=>600];$item2 = ['uid'=>2, 'cate_id'=>2, 'title'=>'如何去除word自动生成目录中的空格', 'pc'=>1200];$data[] = $item1;$data[] = $item2;$res = DB::table('article')->insert($data);var_dump($res);}// insertGetId, 插入并返回idpublic function insert3(){$item = ['uid'=>2, 'cate_id'=>6, 'title'=>'空白页面的问题', 'pc'=>200];$res = DB::table('article')->insertGetId($item);var_dump($res);}// 数据库更新(链式)// pdate() 参数是array// whereIn() 多条public function update1(){$res = DB::table('article')->where('id','=', 8)->update(array('title'=>'update测试'));var_dump($res);}// 数据库删除(链式)// delete() 参数是array// whereIn() 多条public function delete1(){$res = DB::table('article')->where('id','=', 8)->delete();var_dump($res);}
laravel:没有单独创建Model类,但是支持,倾向于把Model类作为与数据库打交道的类。php artisan make:model ArticleDB::table
// Model类<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Article extends Model{// 设置属性指定查询表名// protected $table = 'article';protected $table = 'users';}// 使用模型类// 模型public function mymodel(Article $article){$res = $article->get()->toArray();echo '<pre>';print_r($res);}
项目开发中,大部分功能都要和数据库打交道,学好数据库操作很重要。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号