目前我写了一下,有下面的功能:
取得一个数值[getOne]
取得一行[getRow]
取得所有行返回一个数组[getAll]
普通查询[fetchRow]
更新数据[update]
得到所有的SQL语句赋给数组[sqlStr]
错误处理[dbError]
全局设定Fetch Mode,并可给某个查询设定Fetch Mode
优化数据表[Optimize]
暂时好像就这些,不知道还要什么
取得一个数值[getOne]
取得一行[getRow]
取得所有行返回一个数组[getAll]
普通查询[fetchRow]
更新数据[update]
得到所有的SQL语句赋给数组[sqlStr]
错误处理[dbError]
全局设定Fetch Mode,并可给某个查询设定Fetch Mode
优化数据表[Optimize]
暂时好像就这些,不知道还要什么
数据库抽象层只是起到与数据库沟通的作用,不需要包含太多的功能
具体的操作应该由逻辑层(model)来完成。
大概是这个样子
C也就是控制器来根据要求调用不同的V(也就是视图)
而V是M通用mysql 类(数据抽象访问层)来和数据库交互。生成V反馈给C
我们一般写SQL查询语句的时候不是写在V层,是写在M层
而V层只是调用M层的相关函数就可以实现查询数据库了。
举个例子:
一个会员系统的M层
class member{
var $name; //用户名
var $pwd; //密码
var $db; //数据库抽象层的对象
function member($db)
{
$this->db = $db;
}
function login($name, $pwd) //用户登录程序
{
$sql = 'select * from member where name='$name' and pwd = '$pwd';
$this->db->query($sql);
if ($this->db->total_record()) //相当于mysql_num_rows($res)
return 1;
else
return 0;
}
}
我们在程序中可以这样来实现用户登录
require_once("mysql.php")//包含数据抽象层
require_once("member.php") //包含定义的M层
$db = new mysql();
$mem = new member($db);
if ($mem->login($_POST['name'], $_POST['pwd'])
$_SESSION['name'] = $_POST['name']; //实现登录
else
echo "出错";也就是说M层主要实现了各种对会员的数据库操作的模型
V层只要调用M层就可以。不必考虑M层具体是怎么实现的。
--------------------------------------------------------
以上只是个人对这方面的理解,有不对之处还请指证。
ps:别叫我高手,我也是菜鸟。。
也可放在M层里
function member()
{
$this->db = new mysql();
}
但这样在M层里就必须包含数据类,效果是一样的
我不知道这样好不好,可是我用的顺了,就觉得好了
封装数据库操作的目的是使控制逻辑与具体的数据库操作分离,所以数据库类要能处理各种数据库。也就是你需要了解各种数据库的操作,否则意义并不大。
比如mysql_insert_id在别的数据库中是否提供,如何提供的?
又如在查询串中出现了limit或top,类是否能识别并加以处理?
又如原来使用mysql的程序换用Oracle后数据库相关部分是否要做改动?要做多大改动?
.....
$DSN = array(
"dbHost" => "localhost",
"dbName" => "dbname",
"dbUser" => "root",
"dbPswd" => "",
);
$DB = new DB($DSN);
$row = $DB->getAll("Select * From open_topics");
即可把结果全部给$row了,很方便另外To tianze98(择天) :类并没有编辑SQL,需要把SQL传给方法处理。
<?php
/*
+-------------------------------------------------------------------+
| OpenPHP.cn Version 1.0 Beta |
+-------------------------------------------------------------------+
| Copyright (c) 2003-2004 The OpenPHP.cn Web Team |
+-------------------------------------------------------------------+
| WebSite : http://www.openphp.cn |
| Time : Sat, Apr 17 2004 05:36:04 GMT |
| Support : [email protected] |
| Contect : [email protected] |
| Licence : http://www.openphp.cn/?license |
+-------------------------------------------------------------------+
| Filename : MySQL.class.php |
| Function : DB class |
+-------------------------------------------------------------------+
| Authors : ShenKong <[email protected]> |
| Maintainer : ShenKong <[email protected]> |
| Create Time : Sat, Apr 17 2004 05:36:04 GMT |
| Last Modify : Sat, Apr 17 2004 05:36:04 GMT |
+-------------------------------------------------------------------+
*/ define("DB_FETCH_ROW", MYSQL_NUM);
define("DB_FETCH_ASSOC", MYSQL_ASSOC);
define("DB_FETCH_ARRAY", MYSQL_BOTH); class DB
{
var $dbHost = null;
var $dbName = null;
var $dbUser = null;
var $dbPswd = null;
var $conn = null;
var $query = null;
var $result = null;
var $errStr = null;
var $quick = false;
var $sqlStr = array();
var $sQueries = 0;
var $uQueries = 0; function DB($dsn, $fetchMode = DB_FETCH_ASSOC, $pConn = false)
{
$this->dbHost = $dsn["dbHost"];
$this->dbName = $dsn["dbName"];
$this->dbUser = $dsn["dbUser"];
$this->dbPswd = $dsn["dbPswd"];
$this->connect($pConn);
$this->selectDB();
$this->setFetchMode($fetchMode);
} function connect($pConn = false)
{
$pConn ? $connFunc = "mysql_pconnect" : $connFunc = "mysql_connect";
$this->conn = @ $connFunc($this->dbHost, $this->dbUser, $this->dbPswd);
if (!$this->conn)
{
$this->errStr = "DataBase Connect False : ($this->dbHost, $this->dbUser, ******) !";
$this->dbError();
}
} function selectDB()
{
if ($this->dbName != null)
{
if(! @ mysql_select_db($this->dbName, $this->conn))
{
$this->errStr = "DataBase -[$this->dbName]- does not exist !";
$this->dbError();
}
}
return false;
} function setFetchMode($fetchMode)
{
if(!defined("DB_FETCH_MODE"))
{
define("DB_FETCH_MODE", $fetchMode);
}
} function query($query, $quick = false)
{
$this->quick = $quick;
$this->query = $query;
$this->sqlStr[] = $this->query;
$this->quick ? $queryFunc = "mysql_unbuffered_query" : $queryFunc = "mysql_query";
$this->result = @ $queryFunc($this->query, $this->conn);
$this->sQueries++;
if (!$this->result)
{
$this->dbError();
}
return $this->result;
} function simpleQuery($query, $from = 0, $limit = 0)
{
if($from)
{
$from = $from . ',';
}
if($limit)
{
$query .= ' Limit ' . $from. $limit;
}
$this->query($query);
} function getOne($query)
{
//$this->query = preg_replace("!(select .+ from [^ ]+)(.*)!i", "\\1", $query);
if (!stristr($query, "limit"))
{
$query .= " Limit 1";
}
$this->query($query, true);
$row = $this->fetchRow(DB_FETCH_ROW);
$this->free();
return $row[0];
} function getRow($query, $fetchMode = DB_FETCH_MODE)
{
if (!stristr($query, "limit"))
{
$query .= " Limit 1";
}
$this->query($query, true);
$row = $this->fetchRow($fetchMode);
$this->free();
return $row;
} function getAll($query, $fetchMode = DB_FETCH_MODE)
{
$this->query($query, true);
while($rows = @ $this->fetchRow($fetchMode))
{
$allRows[] = $rows;
}
$this->free();
return $allRows;
} function update($query)
{
$this->query = $query;
$this->sqlStr[] = $this->query;
$this->result = mysql_unbuffered_query($query);
if (!$this->result)
{
$this->errStr = "Update data Error !";
$this->dbError();
}
$this->uQueries++;
$this->free();
return true;
} function getTables()
{
$this->result = @ mysql_list_tables($this->dbName);
if (!$this->result)
{
$this->errStr = "List database's tables Error !";
$this->dbError();
}
$tablesNum = @ mysql_num_rows($this->result);
for ($i = 0; $i < $tablesNum; $i++)
{
$tables[] = mysql_tablename($this->result, $i);
}
return $tables;
} function optimize()
{
$tables = $this->getTables();
print_r($tables);
$tablesNum = count($tables);
for($i=0; $i < $tablesNum; $i++)
{
$this->update("Optimize Table " . $tables[$i]);
echo "Optimeze Table " . $tables[$i] . "\n";
}
} function fetchRow($fetchMode = DB_FETCH_MODE)
{
$rows = @ mysql_fetch_array($this->result, $fetchMode);
return $rows;
} function seek()
{
} function rows()
{
return @ mysql_num_rows($this->result);
} function fields()
{
return @ mysql_num_fields($this->result);
} function lastID()
{
return @ mysql_insert_id($this->conn);
} function free()
{
@ mysql_free_result($this->result);
$this->result = null;
} function close()
{
@ mysql_close($this->conn);
} function dbError()
{
//ob_end_clean();
$errStr = "Error No : " . mysql_errno() . "\n";
$errStr .= "Time : " . date("Y-m-d H:i:s") . "\n";
if (isset($this->errStr))
{
$errStr .= $this->errStr . "\n";
}
if(isset($this->query))
{
$errStr .= "Query : " . $this->query . "\n";
}
$errStr .= "Error MSG : " . mysql_error();
echo nl2br($errStr);
exit;
}
}?>
Class中的$dsn[]数组是从哪取的?
"dbHost" => "localhost",
"dbName" => "dbname",
"dbUser" => "root",
"dbPswd" => "",
);
$DB = new DB($DSN);
$row = $DB->getAll("Select * From open_topics");
这样初始化,我上面已经写过了.
可以方便mysql的操作,不过估计每个人都有一个mysql操作类,只不过功能大小不同而已。
所以觉的意思不是太大,练练手很好。还是一句话:8错8错