Discuz X2二次开发之数据库操作 DB类

Discuz X2的数据库操作类主要包括以下几个:

DB::result_first 返回SQL查询的唯一字段的唯一值,查询结果是字符
DB::fetch_first 返回SQL查询的多个字段的值,查询结果是一个数组
DB::query 执行SQL查询,包括两种,一种是执行update,delete这些修改数据库的操作,还有一种与DB::fetch配合做一个循环查询
DB::fetch 与DB::query和while配合使用,形成一个循环
查询数据表的表写法:".DB::table('除扩展名外的数据表名')." ,说实话这种写法非常浪费时间
首先来说下DB::result_first的用法:
此方法可以作为DB::fetch_first的精简写法,可以一步得到查询结果,例如:
$num = DB::result_first("SELECT count(*) FROM ".DB::table('forum_thread')." WHERE displayorder >=0");
查询结果$num为论坛正常主题的总数
例子:
$subject = DB::result_first("SELECT subject FROM ".DB::table('forum_thread')." where tid= 10000");
$subject = DB::result_first("SELECT subject FROM ".DB::table('forum_thread')." WHERE displayorder >=0 order by dateline desc");
第一个查询结果是tid为10000的主题的标题,第二个查询结果是最新一个论坛主题的标题
DB::fetch_first的用法
例子:
$userinfo = DB::result_first("SELECT username,email FROM ".DB::table('common_member')." WHERE uid=1");
查询结果$userinfo是一个数组
$userinfo[username] uid为1的用户的用户名
$userinfo[email] uid为1的用户的email
DB::query的用法
DB::query分为两种:
1、 修改数据库
DB::query("update ".DB::table('forum_thread')." set views=views+1 where tid = 10000");

此查询的结果是把tid为10000的主题的浏览数增加1
DB::query("delete from ".DB::table('forum_thread')." where tid = 10000");

此查询的结果是删除tid为10000的主题
DB::query("insert into ".DB::table('common_tag')." (tagname,status) values ('标签名称','0')");

此查询的结果是在标签主表里面增加一个标签“标签名称”,关于插入数据库的高级用法会在下面的节里面介绍
2、与DB::fetch配合做循环查询

例子:
$query = DB::query("SELECT tid,subject FROM ".DB::table('forum_thread')." WHERE displayorder >=0 order by dateline desc limit 10");

while($thread = DB::fetch($query)) {

echo '<a href="forum.php?mod=viewthread&tid='.$thread[tid].'" target="_blank">'.$thread[subject].'</a><br>'

}
帮助
查询结果为最新的10个主题的加帖子链接的帖子标题换行显示
注意:老版Discuz只有fetch_array,没有fetch,新版Discuz X2只有fetch,没有fetch_array,搞不清楚官方为什么要换写法,实则作用不大。

Discuz X2查询数据库并分页的程序写法和模板写法


<pre name="code" class="php"><p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(110,110,110); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; background-color:rgb(246,246,237)"><strong><span style="color:rgb(110,110,110); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; line-height:25px; background-color:rgb(246,246,237)"></span></strong></p><pre name="code" class="php">$num = DB::result_first("SELECT COUNT(*) FROM ".DB::table('forum_thread')." where dateline > $_G[timestamp] - 86400 * 30 and displayorder >=0");
2	 
3	$page = intval($_G['gp_page']);
4	 
5	$perpage = 20; //每页显示数量
6	 
7	$page = max(1, intval($page));
8	 
9	$start_limit = ($page - 1) * $perpage;
10	 
11	$theurl = "plus_new.php?action=list"; //分页的链接前缀
12	 
13	$multi = multi($num, $perpage, $page, $theurl);
14	 
15	$threadlist = array();
16	 
17	$query = DB::query("SELECT tid,subject,dateline FROM ".DB::table('forum_thread')." where dateline > $_G[timestamp] - 86400 * 30 and displayorder >=0 order by dateline desc LIMIT $start_limit, $perpage ");
18	 
19	while($thread = DB::fetch($query)) {
20	 
21	$thread['dateline'] = dgmdate($thread['dateline']);
22	 
23	$threadlist[] = $thread;
24	 
25	}
26	 
27	include_once template("diy:plus/plus_new"); //调用模板 需要在template\default 目录下新建plus目录并新建一个plus_new.htm文件</pre><pre name="code" class="php"></pre>模板写法<p></p><pre name="code" class="html"><!--{if $threadlist}-->
28	 
29	<!--{loop $threadlist $thread}-->
30	 
31	<a href="forum.php?mod=viewthread&tid=$thread[tid]" target="_blank">$thread[subject]</a>
32	 
33	$thread[dateline] <br>
34	 
35	<!--{/loop}-->
36	 
37	<!--{else}-->
38	 
39	暂无帖子
40	 
41	<!--{/if}-->
42	 
43	<!--{if $multi}--><div>$multi</div><!--{/if}--></pre><br>
44	<br>
45	<pre></pre>
46	<pre></pre>
47	<pre></pre>
48	<pre></pre>
49	<pre></pre>
50	<pre></pre>
51	<pre></pre>
52	<pre></pre>
53	<pre></pre>
54	 
55	</pre>


转载于:https://my.oschina.net/fedde/blog/138158

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值