Discuz!X3.1数据库的操作(二)
2017-09-09 16:29
309 查看
数据库自定义query
方法名:BD::query()参数解释:
$sql:自定义SQL语句
$arg:需要绑定的数据
$unbuffered:是否使用无缓存查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php DB::query("SELECT * FROM %t WHERE id IN (%n)",array( 'test_db', array(1,2,3) )); //资源集转换结果集 whlie($res = DB::fetch($query)){ $result[] = $res; } debug($result) //自定义删除id=11数据 DB::query("delete from %t where dId = %d",array( 'test_db',9 )); ?> |
资源集转换结果集
方法名:DB::fetch()参数解释:
$resourceid : 数据库查询的query资源
$type : 数组类型
关联索引
1 2 3 4 5 | //资源集转换结果集 whlie($res = DB::fetch($query)){ $result[] = $res; } debug($result) |
1 2 3 4 5 6 7 8 9 10 | <?php $data = DB::query("select * from %t where dId in(%n)",array( 'test_db',array(1,2,3,5) )); while($res = DB::fetch($data,MYSQL_NUM)){ $result[] = $res; } print_r($result); ?> |
1 2 3 4 5 6 7 8 9 10 | <?php $data = DB::query("select * from %t where dId in(%n)",array( 'test_db',array(1,2,3,5) )); while($res = DB::fetch($data,MYSQL_BOTH)){ $result[] = $res; } print_r($result); ?> |
单字段资源集转换为结果集
参数解释:$resourceid : 数据库查询的query资源
$row : 指定行的第一个字段
1 2 3 4 5 6 7 8 9 | <?php $query = DB::query("select * from %t where dId in(%n)",array( 'test_db',array(1,2,3,5) )); $data = DB::result($query,0); echo $data; ?> |
资源集行数计算
方法名:DB::num_rows()参数解释:
$resourceid : 数据库查询的query资源
1 2 3 4 5 6 7 8 9 10 | <?php //小数据量用num_rows,大数据用count(*) $query = DB::query("SELECT * FROM %t WHERE id > %d",array( 'test_db', 7 )); $data = DB::num_rows($query); echo $data; ?> |
资源集资源释放
方法名:DB::free_result()参数解释:
$query:执行SQ语句的query资源
1 2 3 4 5 6 7 8 9 | <?php $query = DB::query("SELECT count(*) FROM %t WHERE id < %d ORDER BY id",array( 'test_db', 7 )); $data = DB::result($query); DB::free_result($query); echo $data; ?> |
按字段排序
方法名:DB::order()参数解释:
$field:需要排序的字段
$order:排序的方式
1 2 3 4 5 6 7 8 9 10 11 12 | <?php //倒序排列 $query = DB::query("select * from %t where dId < %d order by".DB::order('dId','DESC'),array( 'test_db', 8 )); while($res = DB::fetch($query)){ $result[] = $res; } debug($result); ?> |
取值区间设定
方法名:DB::limit()参数解释:
$start:开始的索引值
$limit:条目数
1 2 3 4 5 6 7 8 9 10 11 12 | <?php //用DB::limit()取出2,3,4条数据,如取出前3条就写DB::limit(3) $query = DB::query("SELECT count(*) FROM %t WHERE id < %d ORDER BY".DB::order('id','DESC').DB::limit(2,3,4),array( 'test_db', 10 )); whlie($res = DB::fetch(%query)){ $result[] = $res; } debug($result) ?> |
字段拼接
方法名:BD::implode()参数解释:
$array:需要拼接的字段数组
$glue:字段拼接的字符串
1 2 3 | <?php DB::implode(array('id' => 10, 'name' => 'ddd'), 'and'); ?> |
1 2 3 4 5 6 7 8 9 | <?php DB::query("update %t set".DB::implode(array( 'dName' => 'ccc', 'dId' => '13' )).'where dId=%d',array( 'test_db', 8, )); ?> |
字段数据设定
方法名:DB::field()参数解释:
$field:需要处理的字段名称
$val:字段对应的值
$glue:连接字段与值的类型
1 2 3 4 5 6 | <?php //id=3改为id=99 DB::query("update %t set".DB::field('dId','99','=')."where dId=%d",array( 'test_db',3) ); ?> |
相关文章推荐
- Discuz!X3.1数据库的操作(一)
- Discuz!X3.1数据库的操作(三)
- Discuz!数据库操作DB类和C::t类介绍
- discuz X3.2 DB:类数据库操作
- 数据操作__Discuz X2二次开发之数据库操作 DB类
- 数据库删除discuz 部分数据操作
- discuz插件操作数据库实例
- Discuz X2二次开发之数据库操作
- discuz学习笔记:以easyUI DataGrid为前台进行数据库增查删改操作
- Discuz!数据库操作DB类和C::t类介绍
- Discuz!X3.1后台操作技巧(整合)
- discuz插件操作数据库实例
- discuz数据库操作,记录备用
- Discuz!数据库操作DB类和C::t类介绍
- Discuz!X/数据库操作函数
- (菜鸟篇) 浅谈DISCUZ X系列 数据库的操作
- ACCESS 操作数据库代码
- java程序员从笨鸟到菜鸟之(七)一—java数据库操作
- hibernate操作数据库遇到问题的时候检查主键生成策略
- java数据库操作基本流程