Discuz X2二次开发之数据库操作
2012-12-11 17:14
323 查看
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配合做循环查询
例子:
帮助
查询结果为最新的10个主题的加帖子链接的帖子标题换行显示
注意:老版Discuz只有fetch_array,没有fetch,新版Discuz X2只有fetch,没有fetch_array,搞不清楚官方为什么要换写法,实则作用不大。
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配合做循环查询
例子:
帮助
1 2 3 4 5 6 7 | $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>' } |
注意:老版Discuz只有fetch_array,没有fetch,新版Discuz X2只有fetch,没有fetch_array,搞不清楚官方为什么要换写法,实则作用不大。
Discuz X2查询数据库并分页的程序写法和模板写法
程序部分
帮助
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 | $num =DB::result_first( "SELECT COUNT(*) FROM " .DB::table( 'forum_thread' ). " where dateline > $_G[timestamp] - 86400 * 30 and displayorder >=0" ); $page = intval ( $_G [ 'gp_page' ]); $perpage =20; //每页显示数量 $page =max(1, intval ( $page )); $start_limit =( $page - 1) * $perpage ; $theurl = "plus_new.php?action=list" ; //分页的链接前缀 $multi =multi( $num , $perpage , $page , $theurl ); $threadlist = array (); $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 " ); while ( $thread =DB::fetch( $query )) { $thread [ 'dateline' ] =dgmdate( $thread [ 'dateline' ]); $threadlist [] = $thread ; } include_once template( "diy:plus/plus_new" ); //调用模板 需要在template\default 目录下新建plus目录并新建一个plus_new.htm文件 |
帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!--{if $threadlist}--> <!--{loop $threadlist $thread}--> <a href="forum.php?mod=viewthread&tid=$thread[tid]" target="_blank">$thread[subject]</a> $thread[dateline] <br> <!--{/loop}--> <!--{else}--> 暂无帖子 <!--{/if}--> <!--{if $multi}--><div>$multi</div><!--{/if}--> |
相关文章推荐
- 数据操作__Discuz X2二次开发之数据库操作 DB类
- 使用android快速开发框架afinal的FinalDb操作android数据库
- SQL2K数据库开发二十七之存储过程操作创建存储过程(二)
- 数据库开发基本操作-配置SQL Server 2005 Express的身份验证方式,以及如何启用sa登录名
- Android开发中对数据库的操作
- discuzx3.2自定义积分操作日志,discuzx积分二次开发完全记录
- 使用android快速开发框架afinal的FinalDb操作android数据库
- 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑定)
- SQL2K数据库开发五之表操作管理用户定义的数据类型
- SQL2K数据库开发八之表操作创建主键
- 开发中实用的JDBC连接数据库做操作
- PHP网站开发实例教程之知识点归纳2(操作数据库)
- (菜鸟篇) 浅谈DISCUZ X系列 数据库的操作
- JAVA操作数据库方式与设计模式应用-Java基础-Java-编程开发
- 在开发ajax程序的时候,如何方便地进行数据库操作
- 用flask开发个人博客(21)—— 在视图函数中操作数据库
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
- PHP开发-最简单的数据库操作,使用ezSQL
- 数据库开发基本操作-SQL Server 2005 不允许远程连接解决方法
- discuz X3.2 DB:类数据库操作