野生大熊猫

discuz开发手册-常用函数和方法
常用:获取数组$a = DB::fetch_all("SELECT title FROM "....
扫描右侧二维码阅读全文
02
2018/01

discuz开发手册-常用函数和方法

常用:

获取数组

$a = DB::fetch_all("SELECT title FROM ".DB::table('abc')." WHERE uid = '".$uid."'");

获取对应数据表中一条数据的值返回为数组

$a = DB::fetch(DB::query("SELECT * FROM ".DB::table('abc')." WHERE id = '".$id."'"));

单个数据的调用返回为字符串

$a = DB::result(DB::query("SELECT qi FROM ".DB::table('abc')." WHERE id = '1'"));

统计数据返回为int

$a = DB::result(DB::query("SELECT count(*) FROM ".DB::table('abc')." WHERE id = '1'"));

写入数据(表名,键值数组,条件)

DB::insert('abc', $setarr, 1);

更新数据(表名,键值数组,条件)

DB::update('abc', $setarr, 1);

删除数据(表名,键值数组,条件)

DB::delete('abc', $setarr, 1);

其他不怎么用的手册

DB::table($table) //获取带前缀的表名  
DB::delete($table, $condition, $limit = 0, $unbuffered = true) //删除数据  
DB::insert($table, $data, $return_insert_id = false, $replace = false, $silent = false) //插入数据  
DB::update($table, $data, $condition, $unbuffered = false, $low_priority = false)//更新  
DB::insert_id()//返回插件的自增长id  
DB::fetch_first($sql, $arg = array(), $silent = false) //取查询的第一条数据fetch  
DB::fetch_all($sql, $arg = array(), $keyfield = '', $silent=false) //查询并fetch  
DB::result_first($sql, $arg = array(), $silent = false)//查询结果集的第一个字段值  
DB::query($sql, $arg = array(), $silent = false, $unbuffered = false) //普通查询 一条sql语句
DB::limit($start, $limit = 0) //limit条件  
DB::order($field, $order = 'ASC')//返回排序  
DB::field($field, $val, $glue = '=')//返回字段条件 $val可以为数组  

数据库类定义在目录\source\class\table\
使用方法:C::t('tablename')->method();
插件新增的表mytablename
放置在目录:source/plugin/mypluginid/table/table_mytablename.php
使用类名:table_mytablename
使用用法:C::t('#mypluginid#mytablename')->method();

数据库常用方法

常用词汇

select 选择;count总数;table 表;where 条件;result 结果;perpage 每页;curpage 当前页; update 更新;delete 删除;insert 写入; dateline 日期;time 时间;multi分页函数;if 如果;else 否则;start 起始;limit限制;DESC 倒序;ASC正序;

调用单条数据

$qishi = DB::result(DB::query("SELECT qi FROM " .DB::table('jiangchi')."WHERE id = '1'));
$qishi ;qi:字段;jiangchi :表 ;id = 1 :条件;

调用统计数据

$qishi = DB::result(DB::query("SELECT count(*) FROM " .DB::table('jiangchi')."WHERE id = '1'));

单数据表调用

$perpage = 20;//页数
$curpage = empty ( $_GET['page'] ) ? 1 : intval ( $_GET['page'] );//获取当前页代码
$start = ($curpage-1)*$perpage;//获取起始数据
$askcount = DB::result(DB::query("SELECT COUNT(*) FROM ".DB::table('forum_thread')." WHERE fid = '2' AND authorid > 1 AND FROM_UNIXTIME(dateline, '%Y%m%d') = '$jintian'"));
//$asklist
$asklist = array();
if ($askcount) {//如果有数据
    $query = DB::query("SELECT * FROM ".DB::table('forum_thread')." WHERE fid = '2' AND displayorder > -1 ORDER BY tid ASC LIMIT $start,$perpage");//根据tid正序排序,limit起始到显示
    while ($value = DB::fetch($query)) {
        $asklist[] = $value;
    }
}
$multi = multi($askcount, $perpage, $curpage, "portal.php?mod=topic&topic=ask");

获取数组

$add = DB::fetch_first("SELECT * FROM ".DB::table('peach_webmaster')." WHERE uid=".$uid);

多数据表调用

$perpage = 40;
$curpage = empty ( $_GET['page'] ) ? 1 : intval ( $_GET['page'] );
$start = ($curpage-1)*$perpage;
$acount = DB::result(DB::query("SELECT count(*) FROM ".DB::table('forum_forum')." b LEFT JOIN ".DB::table('forum_forumfield')." bf ON bf.fid=b.fid WHERE b.type='sub' AND b.status = 3 AND bf.icon != ''"));
//$alist
$alist = array();
if ($acount) {
    $query = DB::query("SELECT bf.*, b.* FROM ".DB::table('forum_forum')." b LEFT JOIN ".DB::table('forum_forumfield')." bf ON bf.fid=b.fid WHERE b.type='sub' AND b.status = 3 AND bf.icon != '' ORDER BY bf.shoplevel DESC, b.commoncredits DESC, bf.fid DESC LIMIT $start,$perpage");
    while ($value = DB::fetch($query)) {
        $alist[] = $value;
    }
}
$multi = multi($acount, $perpage, $curpage, "这里填写跳转地址");

前台数据显示

<!--{loop $alist $key $value}-->
<!--{eval $tupianfm = DB::result(DB::query("SELECT attachment FROM ".DB::table('forum_threadimage')." WHERE tid = '$value[tid]'"));}-->
$value[authorid]
<!--{/loop}-->

人性化时间戳

<!--{echo dgmdate(这里填写时间参数, 'u', '9999', getglobal('setting/dateformat'))}-->
<!--{echo date("Y-m/d H:i:s",这里填写时间参数)}-->

截取字符字数

<!--{echo cutstr(这里填写参数,40)}-->

写入数据库

if(submitcheck('tijiao')) {
    $setarr = array(
        'tid' => $_GET['topicid'],
        'name' => $_POST['name'],
        'position' => $_GET['position'],
        'dateline' => $_G['timestamp'],
    );
    DB::insert('forum_poststick', $setarr, 1);
    $query = DB::query("UPDATE ".DB::table('forum_thread')." SET stickreply='1' 
        WHERE tid='$tid'");
    $query = DB::query("DELETE FROM ".DB::table('forum_post')." 
        WHERE pid='$pid'");
    showmessage('成功的提示信息', "forum.php?mod=viewthread&tid=$tid");
}

更新数据库

        $setarr = array(
            'uid' => $uid,
            'name' => $name,
            'url' => $url,
            'datatime' => $datatime,
        );
        DB::update('peach_webmaster',$setarr,1);

表单提交:

<form action="do.php?op=jiangchi&con=yes" method="post" autocomplete="off">
    <input type="hidden" value="{FORMHASH}" name="formhash" />
    最低奖金:<input name="qi" type="text" value="$qishi" /> 
    最高奖金:<input name="end" type="text" value="$zhzh" />  
    <button value="true" name="tijiao" type="submit">提交设置</button>
    <input type="hidden" name="tijiao" value="true" />
</form>

以下两个时间格式是对等关系

date("Ymd",time()); 20150212
FROM_UNIXTIME(dateline, '%Y%m%d')
前者用于PHP,后者用于数据库调用的字段的格式化

调用图片附件

<!--{eval $biaoid = substr($value[tid], -1); $cover = DB::result(DB::query("SELECT attachment FROM ".DB::table('forum_attachment_'.$biaoid.'')." WHERE tid = '$value[tid]'"));}-->
Last modification:January 2nd, 2018 at 07:45 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment