ADOdb类库及其简单应用

分页:

<body>
<?php

include('../adodb5/adodb.inc.php');
include('../adodb5/tohtml.inc.php');     //载入tohtml.inc.php文件
$conn = NEWADOConnection('mysql');
$conn -> Connect('localhost','root','123','shopping');
$conn -> execute('set names gb2312');

$sql = 'select * from admin';     //查询语句
$num = 2;           //每页显示的记录数
if(isset($_GET['n_page'])){       //判断当前页码
   $c_page = $_GET[n_page];      //将$n_page赋给变量$c_apge
}else{
   $c_page = 1;         //初始化变量$c_page
}
$rst = $conn -> PageExecute($sql,$num,$c_page); //执行pageExecute函数

if(false !== $rst){
   if(!$rst -> AtfirstPage()){      //如果当前页不是首页
?>
<!-- 输出向上翻页超链接 -->
   <a href ="<?php echo '?n_pge=1' ?>"> 首页 </a>
   <a href ="<?php echo '?n_page='.($rst -> absolutePage() - 1); ?>"> 上一页 </a>
<!-- ---------------------------- -->
<?php
   }
   if(!$rst -> AtlastPage()){      //如果当前页不是尾页
?>
<!-- 输出向下翻页超链接 -->
   <a href = "<?php echo '?n_page='.($rst -> absolutePage() + 1); ?>"> 下一页 </a>
   <a href ="<?php echo '?n_page='.($rst -> LastPageNo());?>"> 尾页 </a>
<!-- ----------------------------- -->

<?php
   }
   rs2html($rst,'width=350 border=2 bordercolor=blue',array('id','用户名','密码','权限'));
}
?>
<font color='#999999'>当前是第<?php echo $rst -> absolutePage(); ?>页/一共是<?php echo $rst -> LastPageNo(); ?>页</font>
</body>

输出:

处理事务:

<?php
include_once '../adodb5/adodb.inc.php';
$conn = NEWADOConnection('mysql');
$conn -> Connect('localhost','root','123','shopping');
$conn -> execute('set names gb2312');

$conn -> BeginTrans();
$sql = 'delete from admin where adminid = 5';
$rst = $conn -> execute($sql) or die('execute error: '.$conn -> ErrorMsg());
$num = $conn -> Affected_rows();
if($rst){
   if($num != 0){
    $conn -> CommitTrans();
    echo '删除成功!';
    exit();
   }else{
    echo '没有数据,或数据已删除';
    exit();
   }
}else{
   $conn -> RollbackTrans();
   echo '出现意外。';
}
?>

顺遍再抄一段操作数据库的类,便于以后粘贴。

conn.php

<?php
/*数据库类
*/
include_once '../adodb5/adodb.inc.php';
class ado_operator{
/* 连接数据信息 */
private $Database = 'mysql';     //默认数据库驱动
private $Host = 'localhost';    //服务器地址
private $Dataname = 'db_shop';    //数据库名称
private $User = 'root';      //用户名
private $Pwd = '123';      //密码
/* 操作数据库 */
private $conn = '';       //连接数据库
private $rst = '';       //查询结果集
private $error = '';      //错误信息
private $get_rows = 0;      //结果集行数
private $get_fields = 0;     //结果字段数
/* 构造函数 */
function __construct(){
   $this->connection();
}
/* 析构函数 */
function __destruct(){

}
/* 连接数据库 */
function connection($Host = '',$User = '',$Pwd = '',$Dataname = ''){
   if ('' == $Host){
    $Host = $this->Host;
   }
   if ('' == $Dataname){
    $Dataname = $this->Dataname;
   }
   if ('' == $User){
    $User = $this->User;
   }
   if ('' == $Pwd){
    $Pwd = $this->Pwd;
   }
   $this->conn = @ADONewConnection($this->Database);
   @$this->conn->PConnect($Host,$User,$Pwd,$Dataname);
   $this->conn->execute('set names gb2312');
   return $this->conn;
}
/* 查询数据 */
function query_rst($sql){
   if('' == $this->conn){
    $this->connection();
   }
   $this->rst = $this->conn->execute($sql);
   if(false == $this->rst){
    $this->msg('查询语句错误!');
   }
   return $this->rst;
}
/* 添加数据 */
function add_rst($sql,$addarr){
   if('' == $this->conn){
    $this->connection();
   }
   $this->rst = $this->conn->execute($sql);
   $this->insertSQL = $this->conn->getInsertSQL($this->rs,$this->addarr);
   $this->conn->execute($this->insertSQL);
}
/* 更新数据 */
function update_rst($sql,$updatearr){
   if('' == $this->conn){
    $this->connection();
   }
   $this->rst = $this->conn->execute($sql);
   $this->updateSQL = $this->conn->getUpdateSQL($this->rs,$this->updatearr);
   $this->conn->execute($this->updateSQL);
}
/* 删除数据 */
function deldate_rst($sql){
   if('' == $this->conn){
    $this->connection();
   }
   $this->conn->execute($sql);
}
/* 返回结果集行数 */
function get_rows(){
   $this->get_rows = $this->rst->RecordCount();
   return $this->get_rows;
}
/* 返回结果集字段数 */
function get_fields(){
   $this->get_fields = $this->rst->FieldCount();
   return $this->get_fields;
}

/* 错误处理 */
function msg($err){
   $this->error = $this->conn->ErrorMsg();
   echo '<strong>数据库错误:'.$err.'。错误原因:'.$this->error.'</strong>';
}
}

?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值