Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
$res=Db::query("SELECT * FROM `shop_goods` " );
if(!empty($res)){
foreach($res as $key){
echo $key['title'];
echo '<br>';
}
}
printf('<pre>%s</pre>',print_r($res,true));
原声mysql 添加 和修改execute 方法用于执行 MySql 新增和修改操作
$res = Db::execute("SELECT * FROM `shop_goods` ");
// echo $res;
$res = Db::execute("INSERT INTO `lx`.`shop_goods` (`id`, `cat`, `title`, `price`, `discount`, `stock`, `status`, `add_time`) VALUES (null, 1, '内衣学姐风', 300.00, 0, 100, 1, 1576080000); ");
// echo $res
$res = Db::execute("UPDATE `lx`.`shop_goods` SET `add_time` = 1380013801 WHERE `id` = 24; ");
// echo $res
$res = Db::table('shop_goods')->select();
if(!empty($res)){
foreach($res as $key){
echo $key['title'];
echo '<br>';
}
}
printf('<pre>%s</pre>',print_r($res,true));
$res = Db::table('shop_goods')->find('1');
printf('<pre>%s</pre>',print_r($res,true));
$res = Db::table('shop_goods')->value('title');
//printf('<pre>%s</pre>',print_r($res,true));
$res = Db::table('shop_goods')->where('id',20)->value('title');
// printf('<pre>%s</pre>',print_r($res,true));
$res = Db::table('shop_goods')->column('id');
// printf('<pre>%s</pre>',print_r($res,true));
id 为索引进行查询
// $ress = Db::table('shop_goods')->column('title','id');
// printf('<pre>%s</pre>',print_r($ress,tr
insert 插入
arr =[
'id'=>'',
'cat'=>'2',
'title'=>'苹果X',
'price'=>'1688',
'discount'=>'200',
'stock'=>'300',
'status'=>'1',
'add_time'=>'123',
];
$res = Db::table('shop_goods')->insert($arr);
printf('<pre>%s</pre>',print_r($res,true));
insertGetId 查询插入的ID
$arr =[
'id'=>'',
'cat'=>'2',
'title'=>'苹果X',
'price'=>'1688',
'discount'=>'200',
'stock'=>'300',
'status'=>'1',
'add_time'=>'123',
];
$res = Db::table('shop_goods')->insertGetId($arr);
printf('<pre>%s</pre>',print_r($res,true));
insertAll 插入多条数据
//insertAll 插入多条数据
$arr =[
[
'id'=>'',
'cat'=>'2',
'title'=>'苹果X',
'price'=>'1688',
'discount'=>'200',
'stock'=>'300',
'status'=>'1',
'add_time'=>'123',
],
[
'id'=>'',
'cat'=>'2',
'title'=>'苹果12',
'price'=>'1588',
'discount'=>'200',
'stock'=>'300',
'status'=>'1',
'add_time'=>'123',
],
[
'id'=>'',
'cat'=>'2',
'title'=>'苹果13',
'price'=>'168',
'discount'=>'200',
'stock'=>'300',
'status'=>'1',
'add_time'=>'123',
],
];
$res = Db::table('shop_goods')->insertAll($arr);
printf('<pre>%s</pre>',print_r($res,true));
$res = Db::table('shop_goods') ->where('id',42)->update([
'title'=>'隔壁老王手机'
]);
where 条件查询
//默认的是等于
//$res = Db::table('shop_goods') ->where('id',40)->select();
//大于 >
//$res = Db::table('shop_goods') ->where('id','>',10)->select();
//小于<
//$res = Db::table('shop_goods') ->where('id','<',18)->select();
//大于等于
//$res = Db::table('shop_goods') ->where('id','>=',10)->select();
//小于等于
//$res = Db::table('shop_goods') ->where('id','<=',10)->select();
//printf('<pre>%s</pre>',print_r($res,true));
field 字段返回值
res = Db::table('shop_goods') ->field(['title','id'])->select();
printf('<pre>%s</pre>',print_r($res,true));
$arr=['ouyangke'=>'欧阳克'];
// printf('<pre>%s</pre>',print_r($arr,true));
// echo $arr['ouyangke'];
// order 排序
// $res = Db::table('shop_goods') ->order('id DESC')->select();
// printf('<pre>%s</pre>',print_r($res,true));
$res = Db::table('shop_goods') ->field(['title','id'])->limit(0,5)->select();
printf('<pre>%s</pre>',print_r($res,true));
//page 翻页 更方便
$res = Db::table('shop_goods')->order('id DESC')->page(3,3)->select(); printf('<pre>%s</pre>',print_r($res,true));