Discuz!X3.1数据库的操作(二)

数据库自定义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');
?>

将id修改成13,name值修改成ccc

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)
    );
?>
原文地址:https://www.cnblogs.com/alleyonline/p/7498576.html