标签归档:thinkphp5

thinkphp5的crud基本操作理解

thinkphp的参数绑定:
Db::execute(‘insert into think_data (id, name ,status) values (?, ?, ?)’, [8, ‘thinkphp’, 1]);

thinkphp的占位符绑定:
Db::execute(‘insert into think_data (id, name , status) values (:id, :name, :status)’, [‘id’ => 10, ‘name’ => ‘thinkphp’, ‘status’ => 1]);

$result = Db::query(‘select * from think_data where id=:id’, [‘id’ => 10]);
dump($result);

thinkphp的查询构造器:
Db::table()和Db::name(),一个是完整的表名,一个是表前缀
$list = Db::name(‘data’)->where(‘id’,5)->select();

db助手函数默认会每次重新连接数据库,因此应当尽量避免多次调用。
$db = db(‘data’);
$db->insert([‘id’ => 3, ‘name’ => ‘thinkphp’]);
$list = $db->where(‘id’,20)->select();

链式操作做复杂查询:
$list = Db::name(‘data’)->where(‘status’,1)->field(‘id,name’)->order(‘id’,’desc’)->limit(10)->select();
dump($list);