使用前需要先引入Db模块:
use think\Db
所有的查询使用的都是$result=query('语句')
所有的操作语句都是 $result=execute('语句')
注意使用参数绑定.
eg.
Db::execute('insert into tp_data (id,name,age) values (?,?,?)',[3,'php',21]);
有一点C语言的printf 的味道哦~
还有一种方法是占位符:
eg.
Db::execute('insert into tp_data (id, name,age) values (:id,:name,:age)',['id'=>11,'name'=>'php','age'=>22]);
也可以和上面写法一致:
Db::execute('insert into tp_data (id, name,age) values (:id,:name,:age)',[11,'php',22]);
连接多个数据库在database里配置一个,使用的时候执行
Db::connect('db1')->execute('');
再说一下SQL的另一种使用方法:构造器.
构造器的使用效果和上面的SQL语句类似,但是语法不同.
eg.
Db::table('php_data')->where('id',1)->field('id','data')->order('id',desc')->select();
//注意select()必须放在最后
最后说一下事务:几个sql语句同时执行,如果有一个有异常,就同时撤销.
前提:表是innoDB才支持
Db::transaction(function(){
Db::table('php_data')->delete(1};
Db::table('php_data')->insert(['id'=>404,'data'=>'这个是x号']);
})
和BookShare相似的,分数组传递数据是怎么做到的呢?
$p=0;
do{
$result=Db::name('data')->limit($p,2)->select();
$p+=2;
print_r($result);
}while(count($result>0));