亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

staff表中查詢構(gòu)造器中常用的CURD操作

原創(chuàng) 2019-04-01 10:34:30 421
摘要:創(chuàng)建staff表DROP TABLE IF EXISTS `staff`; CREATE TABLE `staff` (   `staff_id` int(4) unsigned NOT NULL AUTO_INCREMENT COMMENT&nbs
創(chuàng)建staff表
DROP TABLE IF EXISTS `staff`;
CREATE TABLE `staff` (
  `staff_id` int(4) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
  `name` varchar(30) NOT NULL COMMENT '姓名',
  `sex` int(1) unsigned NOT NULL DEFAULT '0' COMMENT '性別0男1女',
  `age` tinyint(4) NOT NULL DEFAULT '25' COMMENT '年齡',
  `salary` int(6) unsigned NOT NULL DEFAULT '3000' COMMENT '工資',
  `delete_time` int(10) NOT NULL,
  `enter_time` int(10) NOT NULL,
  PRIMARY KEY (`staff_id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;

<?php
namespace app\index\controller;
use think\Db;

class Query{
	//讀操作返回的都是二維數(shù)組 沒有滿足條件的記錄 返回的是一個空數(shù)組
	//寫操作返回的事受影響的記錄數(shù)量,如果沒有返回0

	public function find(){
		//查詢單條記錄
		$res =Db::table('staff')->where('staff_id',40)->find();
		dump($res);
	}
	public function select(){
		//查詢滿足條件的多條記錄
		$res = Db::table('staff')
			// ->field(['name'=>'姓名','salary'=>'工資'])
			->where('salary','>','3000')/**查詢條件**/
			->order('salary desc')/**排序**/
			->select();
		dump($res);
	}

	public function insert(){
		$data = [
		 'name'=>'huhu',
		 'sex'=>0,
		 'age'=>49,
		 'salary'=>5300,
		];
		//$num=Db::table('staff')->insert($data);
		$num=Db::table('staff')->data($data)->insert();
		$id=Db::getLastInsID();
		return $num ?'添加成功,id='.$id :'沒有記錄被添加';

		//新增多條記錄
		$data= [
			['name'=>'zhangfei','sex'=>0,'age'=>'48','salary'=>6000],
			['name'=>'guanyu','sex'=>0,'age'=>'49','salary'=>5000],
			['name'=>'liubei','sex'=>0,'age'=>'50','salary'=>7000]
		];
		$num=Db::table('staff')->data($data)->insertAll();
		return $num;
		
	}

	public function update(){
		//不允許無條件更新
		$data=['salary'=>Db::raw('salary+1')];
		$res= Db::table('staff')
			->where('salary','>=','9000')
			->data($data)
			->update();

		return $res;

		$num=Db::table('staff')
		    ->update(['sex'=>1,'staff_id'=>79]);//條件明確的話

	}

	public function delete(){
		//不允許無條件刪除
		//刪除一條
		$num=Db::table('staff')
		    ->delete(79);
		//刪除多條
		$num=Db::table('staff')
		    ->delete([1,2]);
		//條件刪除
		$num = Db::table('staff')
			 ->where('salary','>',1000)
			 ->delete();
	}

}

?>


批改老師:西門大官人批改時間:2019-04-02 09:30:30
老師總結(jié):thinkphp的find方法如果查詢不到數(shù)據(jù),返回的貌似是null

發(fā)布手記

熱門詞條