- 论坛徽章:
- 0
|
我是经行了封装的
具体代码
- function &getDb($query=''){
- $db;
- $username = $fileConfig->getValue( "db_username" );
- $password = $fileConfig->getValue( "db_password" );
- //$host = $fileConfig->getValue( "db_host" );
- $dbname = $fileConfig->getValue( "db_database" );
- $dbcharset = $fileConfig->getValue( "db_character_set" );
- $dbpersistent = $fileConfig->getValue( "db_persistent" );
-
- if (preg_match ("/^select/i",ltrim($query))){
-
- $host_array=$fileConfig->getValue( "db_host_array" );
- $host = $host_array[array_rand ($host_array, 1)];
- else{
-
- $db->Close();
- $host = $fileConfig->getValue( "db_host" );
- }
- if($dbpersistent == true) {
- if( !$db->PConnect( $host, $username, $password, $dbname, $dbcharset )) {
- $message = "Fatal error: could not connect to the database!".
- " Error: ".$db->ErrorMsg();
- throw( new Exception( $message ));
- die();
- }
- }
- else {
- if( !$db->Connect( $host, $username, $password, $dbname, $dbcharset )) {
- $message = "Fatal error: could not connect to the database!".
- " Error: ".$db->ErrorMsg();
- throw( new Exception( $message ));
- die();
- }
- }
- }
复制代码
然后还有一个执行数据库操作的函数
- function Execute( $query,
- $page = DEFAULT_PAGING_ENABLED,
- $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
- {
- // initialize the db when we have to execute the first query,
- // not earlier.
- $this->_initializeDb($query);
- //这里初始话的时候区分sql语句是读还是写,然后掉用上班的连接数据库的类经行读写区分
- // see PDbDriverBase (or one of its drivers like PDbMySQLDriver
- // for details)
- $result = $this->_db->Execute( $query, $page, $itemsPerPage );
-
- // if the query generated an error, write a message to the sql
- // error log file
- if( !$result ) {
- $this->_lastError = $this->_db->ErrorMsg();
- lt_include( PLOG_CLASS_PATH . "class/logger/loggermanager.class.php" );
- $log =& LoggerManager::getLogger( "sqlerr" );
- $error = $this->DbError();
- $log->error( "The following query = \n" .
- $query .
- "generated the following error message = \n" .
- $error );
- }
-
- return( $result );
- }
复制代码
sql语句执行都通过这个execute 函数执行
请帮忙看看,写的时候为什么还会连接着从数据库
[ 本帖最后由 ghewqqq 于 2008-12-3 15:14 编辑 ] |
|