dede数据库的常用操作方法


dedecms操作数据库的各类方法,二次开发都会用到的。

首先,引入common.inc.php文件

require_once (dirname(__FILE__) . "/include/common.inc.php");

1、获取一条记录的内容:

$row = $dsql->GetOne("Select * From dede_* where id = $aid");
echo $row['id'];

2、将查询获取总数输出:

$row = $dsql->GetOne("select count(*) as dd where typeid = $typeid");
echo $row['dd'];//输出总数

3、将查询的若干条记录输出

$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;
}

4、将查询的若干条记录输出

$dsql->SetQuery("Select id,typename From `dede_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;
}

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

6、删除一条记录

$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."';");

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);

8、判断获取数据库内容的常用方法

$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 "失败";
}

9、获取总数

$dsql = new DedeSql(false);
$dsql->SetQuery("Select * from `dede_admin` where userid='$userid' Or uname='$uname'");
$dsql->Execute();
$ns = $dsql->GetTotalRow();
关闭数据库
$dsql->Close();