当前位置: 移动技术网 > IT编程>开发语言>PHP > yii2.0框架数据库操作简单示例【添加,修改,删除,查询,打印等】

yii2.0框架数据库操作简单示例【添加,修改,删除,查询,打印等】

2020年05月12日  | 移动技术网IT编程  | 我要评论
本文实例讲述了yii2.0框架数据库操作。分享给大家供大家参考,具体如下:添加$id = \yii::$app->db->createcommand()->insert('表名',[

本文实例讲述了yii2.0框架数据库操作。分享给大家供大家参考,具体如下:

添加

$id = \yii::$app->db
->createcommand()
->insert('表名',['car_num' => $car_num, 'lg_shop_id' => $shop_id])
->execute();
batchinsert():一次添加多行
// table name, column names, column values
yii::$app->db->createcommand()->batchinsert('user', ['name', 'age'], [
  ['tom', 30],
  ['jane', 20],
  ['linda', 25],
])->execute();

修改

// update (table name, column values, condition)
yii::$app->db->createcommand()->update('user', ['status' => 1], 'age > 30')->execute();


删除

// delete (table name, condition)
yii::$app->db->createcommand()->delete('user', 'status = 0')->execute();

查询条件

$status = 10;
$search = 'yii';
$query->where(['status' => $status]);
if (!empty($search)) {
  $query->andwhere(['like', 'title', $search]);
}


如果 $search 不为空,那么将会生成如下 sql 语句:

... where (`status` = 10) and (`title` like '%yii%')

查询以及打印查询sql

$query = new query();
    $query->from('{{%shop_info}}');
    $query->where('shop_type=1');
    $query->select('shop_name');
    $rea = $query->all();//查询
    $res = $query->createcommand();//打印sql
    echo $res->sql;die;
    var_dump($rea);die;

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网