常用:
获取数组
$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 可以为数组
数据库类定义在目录 sourceclasstable
使用方法: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, "这里填写跳转地址");
前台数据显示
$value[authorid]
人性化时间戳
截取字符字数
写入数据库
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);
表单提交:
以下两个时间格式是对等关系
date("Ymd",time()); 20150212
FROM_UNIXTIME(dateline, '%Y%m%d')
前者用于 PHP,后者用于数据库调用的字段的格式化
调用图片附件