⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqlc.php

📁 太烦了
💻 PHP
📖 第 1 页 / 共 4 页
字号:
                    $tmp = DB_FETCHMODE_DEFAULT;                } else {                    $tmp = $params;                }                $params = $fetchmode;                $fetchmode = $tmp;            } elseif ($params !== null) {                $fetchmode = $params;                $params = array();            }        }        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }            $res =& $this->execute($sth, $params);            $this->freePrepared($sth);        } else {            $res =& $this->query($query);        }        if (DB::isError($res) || $res === DB_OK) {            return $res;        }        $results = array();        while (DB_OK === $res->fetchInto($row, $fetchmode)) {            if ($fetchmode & DB_FETCHMODE_FLIPPED) {                foreach ($row as $key => $val) {                    $results[$key][] = $val;                }            } else {                $results[] = $row;            }        }        $res->free();        if (DB::isError($row)) {            $tmp =& $this->raiseError($row);            return $tmp;        }		$this->saveCache($qry, $results);        return $results;    }    // }}}	/* Query is processed only for the update/delete/insert etc. queries.		THis is needed to update the corresponding tables fully.	 */    // {{{ query()    /**     * Send a query to the database and return any results with a     * DB_result object     *     * The query string can be either a normal statement to be sent directly     * to the server OR if <var>$params</var> are passed the query can have     * placeholders and it will be passed through prepare() and execute().     *     * @param string $query  the SQL query or the statement to prepare     * @param mixed  $params array, string or numeric data to be used in     *                       execution of the statement.  Quantity of items     *                       passed must match quantity of placeholders in     *                       query:  meaning 1 placeholder for non-array     *                       parameters or 1 placeholder per array element.     *     * @return mixed  a DB_result object or DB_OK on success, a DB     *                error on failure     *     * @see DB_result, DB_common::prepare(), DB_common::execute()     * @access public     */    function &query($query, $params = array())    {		settype($params,'array');		$qry = $this->prepareFullQuery($query, $params);		$ismanip = $this->queryisManip($qry);		if ($ismanip) {			$this->updateTableTimes($qry);		}        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }            $ret =& $this->execute($sth, $params);            $this->freePrepared($sth);            return $ret;        } else {            $result = $this->simpleQuery($query);            if (DB::isError($result) || $result === DB_OK) {                return $result;            } else {                $tmp =& new DB_result($this, $result);                return $tmp;            }        }    }    // }}}    // {{{ autoExecute()    /**     * Automaticaly generate an insert or update query and call prepare()     * and execute() with it     *     * @param string $table name of the table     * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value     * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)     * @param string $where in case of update queries, this string will be put after the sql WHERE statement     * @return mixed  a new DB_Result or a DB_Error when fail     * @see DB_common::autoPrepare(), DB_common::buildManipSQL()     * @access public     */    function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = false)    {        $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where);        $ret =& $this->execute($sth, array_values($fields_values));        $this->freePrepared($sth);        return $ret;    }    // }}}    // {{{ execute()    /**     * Executes a DB statement prepared with prepare()     *     * Example 1.     * <code> <?php     * $sth = $dbh->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');     * $data = array(     *     "John's text",     *     "'it''s good'",     *     'filename.txt'     * );     * $res =& $dbh->execute($sth, $data);     * ?></code>     *     * @param resource  $stmt  a DB statement resource returned from prepare()     * @param mixed  $data  array, string or numeric data to be used in     *                      execution of the statement.  Quantity of items     *                      passed must match quantity of placeholders in     *                      query:  meaning 1 placeholder for non-array     *                      parameters or 1 placeholder per array element.     *     * @return object  a new DB_Result or a DB_Error when fail     *     * {@internal ibase and oci8 have their own execute() methods.}}     *     * @see DB_common::prepare()     * @access public     */    function &execute($stmt, $data = array())    {        $realquery = $this->executeEmulateQuery($stmt, $data);        if (DB::isError($realquery)) {            return $realquery;        }		$ismanip = $this->queryisManip($realquery);		if ($ismanip) {			$this->updateTableTimes($realquery);		}        $result = $this->simpleQuery($realquery);        if (DB::isError($result) || $result === DB_OK) {            return $result;        } else {            $tmp =& new DB_result($this, $result);            return $tmp;        }	}    // }}}    /* Cache related functions are added below */	/* Get all tables in osDate and populate the $osDate_tables array. THis will	   check if the file is available in cache and yes, then load the contents.	   Otherwise get all table names from DB and create file without any time and	   store the names in the $cached_tables array with 0 as time. */	/* This function just does the prepare and emulateExecuteQuery and returns		complete query with all replaceable values replaced */	function prepareFullQuery($query, $params = array()) {        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }	        $realquery = $this->executeEmulateQuery($sth, $params);			return $realquery;		}		return $query;	}	function getAllCachedTables() {		global $config;		if ($config['disable_cache'] == 'Y' || $config['disable_cache'] == '1' ) {			return true;		}		if (is_readable(CACHE_DIR.'cached_tables.dat')  ) {			/* OK. The cache file is available. Load it to memory */			$this->cached_tables = unserialize( file_get_contents( CACHE_DIR.'cached_tables.dat' ) );		} else {			$res = $this->simpleQuery("show tables");	        while (DB_OK === $this->fetchInto($res, $row, $fetchmode)) {				foreach ($row as $v) {					$this->cached_tables[$v] = 0;				}			}			$this->writeCachedTables();		}	}	/* This function will write the cacehd tables update time into the file */	function writeCachedTables() {		global $config;		if ($config['disable_cache'] == 'Y' || $config['disable_cache'] == '1' ) {			return true;		}		/* Now write this to cached_tables info file */		$fp = fopen(CACHE_DIR.'cached_tables.dat', 'wb');		fwrite($fp, serialize($this->cached_tables));		fclose($fp);	}	// {{{ update_table_times()	/*	 *	This function will update the cached_tables file with latest update time.	 *  This will write the file with updated time	 *  @param the SQL Query	 *	 *	@access internal	 *  @return none	 */	function updateTableTimes($query) {		global $config;		if ($config['disable_cache'] == 'Y' || $config['disable_cache'] == '1' ) {			return true;		}		$this->getAllCachedTables();		$query = strtolower($query);		$table_found=0;		$tim = (float) $this->get_micro_time1();		if (count($this->cached_tables) > 0) {			foreach ($this->cached_tables as $tab => $tm) {				if (substr_count($query,strtolower($tab)) > 0) {					$table_found++;					$this->cached_tables[$tab]= (float) $tim+2;				}			}		}		if ($table_found == 0) {			/* This is table manipulation query, but table name missing in the array.				Need to add the new table */			$res = $this->simpleQuery("show tables");	        while (DB_OK === $this->fetchInto($res, $row, $fetchmode)) {				foreach ($row as $v) {					if (!isset($this->cached_tables[$v] ) ) {						$this->cached_tables[$v] = 0;					}				}			}		}		$this->writeCachedTables();	}	// }}}	//  {{{ checkCache()	/*	 *	This creates a hash of the query and checks if this hash is already saved. If yes,	 *	it takes the cached time. THen it loads the cached_tables hash and	 *	checks the tables in the query and determine the latest upate time for the	 *	table as given in the cached_tables hash is later than cached_time. If later, it	 *	proceeds with query and saves the result as hash. Otherwise, it takes the cached	 * data and returns	 */	 function checkCache($query)	 {		global $config;		if ($config['disable_cache'] == 'Y' || $config['disable_cache'] == '1' || (isset($_SESSION['AdminId']) && $_SESSION['AdminId'] > 0 )) {		/* Admin. should read data in any case, bypass cache */			return false;		}		/* For shoutbox, no cache file checkng.. */		if (substr_count(strtolower($query),strtolower(SHOUTBOX_TABLE)) > 0 | substr_count(strtolower($query),strtolower(INSTANT_MESSAGE_TABLE)) > 0 || substr_count(strtolower($query),strtolower(BANNER_TABLE)) > 0) {			return false;		}		$ismanip = $this->queryisManip($query);		if ($ismanip) {			$this->updateTableTimes($query);			return false;		}		/* First get all tables from the cached_tables hash */		$this->getAllCachedTables();		/* Get hash file name for the query */		$cached_file_name = $this->generateCacheFilename($query);	 	/* Now see if there is a hash for current query is svailable */		$cached_data = $this->getCachedData($cached_file_name);		if (!$cached_data || !is_array($cached_data) || empty($cached_data)) { return false; }		$cached_time = $cached_data['cached_time'];		$query = strtolower($query);		if (count($this->cached_tables) > 0) {			foreach ($this->cached_tables as $tab => $tm) {				if (substr_count($query,strtolower($tab)) > 0) {					if ($cached_time < $tm) {						$this->removeCacheFile($cached_file_name);						return false;					}				}			}		}		/* Cache is valid. return data */		return $cached_data['saved_data'];	 }	 // }}}	// {{{ saveCache()	/*	 *	This function will update the cached_tables file with latest update time.	 *  This will write the file with updated time	 *  @param the SQL Query, result of query	 *	 *	@access internal	 *  @return none	 */	function saveCache($query, $result) {		global $config;		if ($config['disable_cache'] == 'Y' || $config['disable_cache'] == '1' || $_SESSION['AdminId'] > 0) {			return true;		}		/* For shoutbox, no cache file checkng.. */		if (substr_count(strtolower($query),strtolower(SHOUTBOX_TABLE)) > 0 || substr_count(strtolower($query),strtolower(INSTANT_MESSAGE_TABLE)) > 0 || substr_count(strtolower($query),strtolower(BANNER_TABLE)) > 0) {			return true;		}		if ((!is_array($result) && $result != '') or( is_array($result) && count($result) > 0) ){			$cache_file = $this->generateCacheFilename($query);			$save_array = serialize(array(				'cached_time' => $this->get_micro_time1(),				'saved_data' => $result)				);			$fp = @fopen(CACHE_DIR.$cache_file,'wb');			@flock($fp,LOCK_EX);			@fwrite($fp,$save_array);			@flock($fp,LOCK_UN);			@fclose($fp);		}	}	// }}}	/* This function generates the file name for the cached item */	function generateCacheFilename($input)	{		return 'cache_'.md5($input).".dat";	}	/* Get microtime as table update time and cache time */	function get_micro_time1()	{/*		list($usec, $sec) = explode(" ", microtime());		return (float)($usec + $sec);		*/		return time();	}	/* This function gets the cache file for a given cached_file_name  and returns		data, and time of cache	*/	function getCachedData($cached_file_name){		global $config;		$cached_data = array();		if ($config['disable_cache'] == 'Y' || $config['disable_cache'] == '1' || (isset($_SESSION['AdminId']) && $_SESSION['AdminId'] > 0 ) ) {			return $cached_data;		}		if (file_exists(CACHE_DIR.$cached_file_name)) {			$cached_data = unserialize(file_get_contents(CACHE_DIR.$cached_file_name));		}		return $cached_data;	}	/* This function will remove the cacehd file from CACHE_DIR */	function removeCacheFile($cache_file_name) {		unlink(CACHE_DIR.$cache_file_name);	}    /**     * Tell whether a query is a data manipulation query (insert,     * update or delete) or a data definition query (create, drop,     * alter, grant, revoke).     *     * @access public     *     * @param string $query the query     *     * @return boolean whether $query is a data manipulation query     */    function queryisManip($query)    {        $manips = 'INSERT|UPDATE|DELETE|';        if (preg_match('/^\s*"?('.$manips.')\s+/i', strtoupper($query))) {            return true;        }        return false;    }	function dbconnect() {		if (!$this->_connected) {			$ret = $this->connect();			$this->_connected=true;			return $ret;		}	}}/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -