dede数据库类使用方法 $dsql
dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了。
引入common.inc.php文件
1
|
require_once
(dirname(
__FILE__
) .
"/include/common.inc.php"
);
|
获取一条记录的内容
1
2
|
$row
=
$dsql
->GetOne(
"Select * From dede_* where id = $aid"
);
echo
$row
[
'id'
];
|
将查询获取总数输出
1
2
|
$row
=
$dsql
->GetOne(
"select count(*) as dd where typeid = $typeid"
);
echo
$row
[
'dd'
];
//输出总数
|
将查询的若干条记录输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$sql
=
"Select * from dede_*"
;
$dsql
->SetQuery(
$sql
);
//将SQL查询语句格式化
$dsql
->Execute();
//执行SQL操作
//通过循环输出执行查询中的结果
while
(
$row
=
$dsql
->GetArray()){
echo
$row
[
'id'
];
echo
$row
[
'title'
];
}
//或者采取这种方式输出内容
while
(
$row
=
$dsql
->GetObject()){
echo
$row
->id;
echo
$row
->Title;
}
|
将查询的若干条记录输出dedecms5
1
2
3
4
5
6
7
|
$dsql
->SetQuery(
"Select id,typename From `#@__arctype` where reid=0 And channeltype=1 And ishidden=0 And ispart<>2 order by sortrank"
);
$dsql
->Execute();
while
(
$row
=
$dsql
->GetObject())
{
$channellist
.=
"<a href='wap.php?action=list&id={$row->id}'>{$row->typename}</a> "
;
echo
$row
->id;
}
|
插入一条记录
1
2
3
4
5
|
$sql
= "
INSERT INTO `dede_member_flink`(mid,title,url,linktype,imgurl,imgwidth,imgheight)
VALUES(
".$cfg_ml->M_ID."
,
'$title'
,
'$url'
,
'$linktype'
,
'$imgurl'
,
'$imgwidth'
,
'$imgheight'
);";
//插入记录数据库
$dsql
->SetQuery(
$sql
);
//格式化查询语句
$dsql
->ExecNoneQuery();
//执行SQL操作
|
经实践证明,上面的语句不能正常插入数据库,下面是正确的语句
1
2
3
4
5
|
$sql
= "
INSERT INTO `dede_member_flink`(mid,title,url,linktype,imgurl,imgwidth,imgheight)
VALUES(
".$cfg_ml->M_ID."
,
'$title'
,
'$url'
,
'$linktype'
,
'$imgurl'
,
'$imgwidth'
,
'$imgheight'
);";
//插入记录数据库
$dsql
->ExecuteNoneQuery(
$sql
);
//执行SQL操作
$gid
=
$dsql
->GetLastID();
//获取刚刚插入的id
|
删除一条记录
1
2
3
4
5
|
$sql
=
"Delete From dede_member_flink where aid='$aid' And mid='"
.
$cfg_ml
->M_ID.
"';"
;
$dsql
->SetQuery(
$sql
);
$dsql
->ExecNoneQuery();
//或者使用简化模式
$dsql
->ExecNoneQuery(
"Delete From dede_member_flink where aid='$aid' And mid='"
.
$cfg_ml
->M_ID.
"';"
);
|
更新一条记录
1
2
3
4
5
6
7
|
$upquery
= "
Update dede_member_flink set
title=
'$title'
,url=
'$url'
,linktype=
'$linktype'
,
imgurl=
'$imgurl'
,imgwidth=
'$imgwidth'
,imgheight=
'$imgheight'
where aid=
'$aid'
And mid=
'".$cfg_ml->M_ID."'
;
";
$rs
=
$dsql
->ExecuteNoneQuery(
$upquery
);
|
判断获取数据库内容的常用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$row
=
$dsql
->GetOne(
"Select * From dede_moneycard_type where tid={$pid}"
);
if
(!
is_array
(
$row
)){
echo
"失败"
;
exit
();
}
/
$upquery
= " Update dede_member_flink set
title=
'$title'
,url=
'$url'
,linktype=
'$linktype'
,
imgurl=
'$imgurl'
,imgwidth=
'$imgwidth'
,imgheight=
'$imgheight'
where aid=
'$aid'
And mid=
'".$cfg_ml->M_ID."'
;
";
$rs
=
$dsql
->ExecuteNoneQuery(
$upquery
);
if
(
$rs
){
echo
"成功"
;
}
else
{
echo
"失败"
;
}
|
获取总数
1
2
3
4
|
$dsql
=
new
DedeSql(false);
$dsql
->SetQuery(
"Select * from `dede_admin` where userid='$userid' Or uname='$uname'"
);
$dsql
->Execute();
$ns
=
$dsql
->GetTotalRow();
|
关闭数据库
1
|
$dsql
->Close();
|
实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<?php
/*
DedeCms 数据库使用实例说明
*/
require_once
dirname(
__FILE__
).
"pub_db_mysql.php"
;
//引用数据库文件
//确保数据库信息填写正确
//数据库连接信息
$cfg_dbhost
=
'localhost'
;
$cfg_dbname
=
'sccms'
;
$cfg_dbuser
=
'root'
;
$cfg_dbpwd
=
'123456'
;
$cfg_dbprefix
=
'sc_'
;
$cfg_db_language
=
'utf8'
;
//新建一个数据库操作类
$dsql
=
new
ScSql(false);
获取一条记录的内容///
//下面是获取一条记录使用实例
$row
=
$dsql
->GetOne(
"Select * From dede_* where id = $aid"
);
//获取数据内容保存在数组$row中,通过下标可以将其调用出来
echo
$row
[
'id'
];
//下面是循环调用记录
///
//将查询获取总数输出/
//获取一个查询记录总数
$row
=
$dsql
->GetOne(
"select count(*) as dd where typeid = $typeid"
);
echo
$row
[
'dd'
];
//输出总数
///
//将查询的若干条记录输出//
$sql
=
"Select * from dede_*"
;
$dsql
->SetQuery(
$sql
);
//将SQL查询语句格式化
$dsql
->Execute();
//执行SQL操作
//通过循环输出执行查询中的结果
while
(
$row
=
$dsql
->GetArray()){
echo
$row
[
'id'
];
echo
$row
[
'title'
];
}
//或者采取这种方式输出内容
while
(
$row
=
$dsql
->GetObject()){
echo
$row
->id;
echo
$row
->Title;
}
///
//插入一条记录///
$sql
= "
INSERT INTO `dede_member_flink`(mid,title,url,linktype,imgurl,imgwidth,imgheight)
VALUES(
".$cfg_ml->M_ID."
,
'$title'
,
'$url'
,
'$linktype'
,
'$imgurl'
,
'$imgwidth'
,
'$imgheight'
);";
//插入记录数据库
$dsql
->SetQuery(
$sql
);
//格式化查询语句
$dsql
->ExecNoneQuery();
//执行SQL操作
///
/删除一条记录///
$sql
=
"Delete From dede_member_flink where aid='$aid' And mid='"
.
$cfg_ml
->M_ID.
"';"
;
$dsql
->SetQuery(
$sql
);
$dsql
->ExecNoneQuery();
//或者使用简化模式
$dsql
->ExecNoneQuery(
"Delete From dede_member_flink where aid='$aid' And mid='"
.
$cfg_ml
->M_ID.
"';"
);
///
//更新一条记录//
$upquery
= "
Update dede_member_flink set
title=
'$title'
,url=
'$url'
,linktype=
'$linktype'
,
imgurl=
'$imgurl'
,imgwidth=
'$imgwidth'
,imgheight=
'$imgheight'
where aid=
'$aid'
And mid=
'".$cfg_ml->M_ID."'
;
";
$rs
=
$dsql
->ExecuteNoneQuery(
$upquery
);
///
判断获取数据库内容的常用方法///
$row
=
$dsql
->GetOne(
"Select * From dede_moneycard_type where tid={$pid}"
);
if
(!
is_array
(
$row
)){
echo
"失败"
;
exit
();
}
/
$upquery
= " Update dede_member_flink set
title=
'$title'
,url=
'$url'
,linktype=
'$linktype'
,
imgurl=
'$imgurl'
,imgwidth=
'$imgwidth'
,imgheight=
'$imgheight'
where aid=
'$aid'
And mid=
'".$cfg_ml->M_ID."'
;
";
$rs
=
$dsql
->ExecuteNoneQuery(
$upquery
);
if
(
$rs
){
echo
"成功"
;
}
else
{
echo
"失败"
;
}
//获取总数//
$dsql
=
new
DedeSql(false);
$dsql
->SetQuery(
"Select * from `dede_admin` where userid='$userid' Or uname='$uname'"
);
$dsql
->Execute();
$ns
=
$dsql
->GetTotalRow();
关闭数据库///
$dsql
->Close();
///
?>
|