您的位置:首页 > 数据库

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

2017-09-09 16:28 330 查看
数据库指的是以一定方式储存在一起、能为多个用户共享、具有尽可能小的余度、与应用程序彼此独立的数据集合。产品中的主题,帖子,用户,关注等等。所有数据都存储在数据库中。

官方数据字典

http://faq.comsenz.com/library/database/x3/x3_index.htm

数据库规范

http://dev.discuz.org/wiki/index.php?title=编码规范

数据表插入操作

方法名:DB::insert()

1
2
3
4
5

<?php
DB::insert('test_db',array(
'dname' => 'ppc',
),true);
?>

参数解释:

$table:插入数据的表

$data:插入的数据,字段对应值

$return_insert_id:是否返回插入数据的ID

$replace:是否使用replace into

1
2
3
4
5
6
7

<?php
//$replace 当存在数据执行修改,不存在执行写入
DB::insert('test_db',array(
'did' => '1',
'dname' => 'ppc',
),true,true);
?>

$slient:操作失败是否不提示

数据表删除操作

方法名:DB::delete()

1
2
3

<?php
DB::delete('test_db','dId=4',1,true)
?>

参数解释:

$table:删除数据的表

$condition:删除条件

$limit:删除满足条件的目数

$unbuffered:是否使用无缓存查询

数据表更新操作

方法名:DB::update()

1
2
3
4
5

<?php
DB::update('test_db',array(
'dName' => 'ppc2'
),'dId=5',true);
?>

插入的值如果是变量用array()

1

DB::update('borle_do',array('countMoney' => $countMoney),array('doId'=> $doId),true);}

参数解释:

$table:(更新数据的表)

$data:更新的数据,字段对应的

$condition:更新的条件

$unbuffrerd:是否使用无缓存查询

$low_priority:是否采用无损更新表

绑定查询的参数解释

表达式数据处理
%tDB::table()
%dIntval()
%saddslashes
%nIN(1,2,3)
%fSprintf(‘%f,%var’)
%i不做任何处理

数据表查询操作(单条)

方法名:DB::fetch_first()

1
2
3
4
5
6
7

<?php
$dId = 5;//可以使用$dId = '2admin',输出2,由于Intval()将指转换为2
$data = DB::fetch_first('select * from %t where dId=%d',
array('test_db',$dId)
);
print_r($data);
?>

参数解释:

$sql:查询数据的sql语句

$arg:绑定查询的参数

$silent:查询失败时是否不提示

数据表查询操作(多条)

方法名:DB::fetch_all()

1
2
3
4
5
6
7
8

<?php
$data = DB::fetch_all("select * from %t where dId>=%d and dId<=%d",
array('test_db',1,9),
'dId'
);

print_r($data);
?>

通过%n进行数据查询

1
2
3
4
5
6
7

<?php
//%n表示array(1,2,3,4,5,6)
$data = DB::fetch_all("select * from %t where dId in (%n)",
array('test_db',array(1,2,3,4,5,6))
);
print_r($data);
?>

参数解释:

$sql:查询数据的SQL语句

$arg:绑定查询的参数

$keyfield:一维索引的字段名称

$silent:查询失败的是否不提示

数据表查询操作(单字段)

方法名:DB::result_first()

1
2
3
4
5
6
7

<?php
//查询dName字段id=1的数据
$data = DB::result_first("select dName from %t where dId=%d",
array('test_db',1)
);
echo $data;
?>

比较常用语统计查询

1
2
3
4
5
6

<?php
$data = DB::result_first("select count(*) from %t",
array('test_db')
);
echo $data;
?>

取最大值id

1
2
3
4
5
6

<?php
$data = DB::result_first("select max(dId) from %t",
array('test_db')
);
echo $data;
?>

参数解释:

$sql:查询数据的sql语句

$arg:绑定查询的参数

$silent:查询失败时是否不提示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: