首页上一页 1 下一页尾页 2 条记录 1/1页
请教个问题 php开发到入门的19章 adodb
发表在PHP图书答疑
2009-10-10
是否精华
是
否
版块置顶:
是
否
<?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 = 'root'; //密码
/* 操作数据库 */
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>';
}
}
?>
以上是19.9练习的代码:
这里的增加和修改使用到 了getupdatesql/getinsertsql函数
请问这样写有必要么?因为不用这两个函数
$this->rst = $this->conn->execute([font color=#0000FF]$sql[/font]); 这句话也是可以实现修改或增加的呀?
还有就是 在修改或增加的时候的
$this->updateSQL = $this->conn->getUpdateSQL([font color=#FF0000]$this->rs[/font],$this->updatearr);
$this->rs 是不是写错了 还是应该是$this->rst 可能是我哪里理解有错还望解答。
/*数据库类
*/
include_once '../adodb5/adodb.inc.php';
class ado_operator{
/* 连接数据信息 */
private $Database = 'mysql'; //默认数据库驱动
private $Host = 'localhost'; //服务器地址
private $Dataname = 'db_shop'; //数据库名称
private $User = 'root'; //用户名
private $Pwd = 'root'; //密码
/* 操作数据库 */
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>';
}
}
?>
以上是19.9练习的代码:
这里的增加和修改使用到 了getupdatesql/getinsertsql函数
请问这样写有必要么?因为不用这两个函数
$this->rst = $this->conn->execute([font color=#0000FF]$sql[/font]); 这句话也是可以实现修改或增加的呀?
还有就是 在修改或增加的时候的
$this->updateSQL = $this->conn->getUpdateSQL([font color=#FF0000]$this->rs[/font],$this->updatearr);
$this->rs 是不是写错了 还是应该是$this->rst 可能是我哪里理解有错还望解答。