📄 mysql.php
字号:
<?php/*** @package Library* This file has the MySQLDb class in it.** @version $Id: mysql.php,v 1.4 2005/02/22 03:14:07 chris Exp $* @author Chris <chris@interspire.com>* @filesource*/// Include the base database class.require_once(dirname(__FILE__) . '/db.php');/*** @package Library* @subpackage Db* This is the class for the MySQL database system.*/class MySQLDb extends Db { /** * @var connection The global database connection. */ var $connection = null; /** * Constructor * * Sets up the database connection. * Can pass in the hostname, username, password and database name if you want to. * If you don't it will set up the base class, then you'll have to call Connect yourself. * * @param servername Name of the server to connect to. * @param username Username to connect to the server with. * @param password Password to connect with. * @param databasename Database name to connect to. * * @see Connect * @see GetError * * @return mixed Returns false if no connection can be made - the error can be fetched by the Error() method. Returns the connection result if it can be made. Will return Null if you don't pass in the connection details. */ function MySQLDb($hostname='', $username='', $password='', $databasename='') { if ($hostname && $username && $databasename) { $connection = &$this->Connect($hostname, $username, $password, $databasename); return $connection; } return null; } /** * Connect * * This function will connect to the database based on the details passed in. * * @param servername Name of the server to connect to. * @param username Username to connect to the server with. * @param password Password to connect with. * @param databasename Database name to connect to. * * @see SetError * * @return mixed Will return the connection if it's successful. Otherwise returns false. */ function Connect($hostname='', $username='', $password='', $databasename='') { if ($hostname == '') { $this->SetError('No server name to connect to'); return false; } if ($username == '') { $this->SetError('No username name to connect to server ' . $hostname . ' with'); return false; } if ($databasename == '') { $this->SetError('No database name to connect to'); return false; } if (!$connection_result = mysql_connect($hostname, $username, $password)) { $this->SetError(mysql_error()); return false; } $this->connection = &$connection_result; if (!$db_result = mysql_select_db($databasename, $connection_result)) { $this->SetError('Unable to select database \'' . $databasename . '\': ' . mysql_error()); return false; } return $this->connection; } /** * Disconnect * * This function will disconnect from the database handler passed in. * * @param Resource Resource to disconnect from * * @see SetError * * @return mixed Will return the connection if it's successful. Otherwise returns false. */ function Disconnect($resource=null) { if (is_null($resource)) { $this->SetError('Resource is a null object'); return false; } if (!is_resource($resource)) { $this->SetError('Resource ' . $resource . ' is not really a resource'); return false; } $close_success = mysql_close($resource); return $close_success; } /** * Query * * This function will run a query against the database and return the result of the query. * * @see LogQuery * @see SetError * * @return mixed Returns false if the query is empty or if there is no result. Otherwise returns the result of the query. */ function Query($query='') { $this->LogQuery($query); if (!$query) { $this->SetError('Query passed in is empty'); return false; } if (!$this->connection) { $this->SetError('No valid connection'); return false; } $result = mysql_query($query); if (!$result) { $this->SetError(mysql_error()); return false; } return $result; } /** * Fetch * * This function will fetch a result from the result set passed in. * * @param resource The result from calling Query. Returns an associative array (not an indexed based one) * * @see SetError * * @return mixed Returns false if the result is empty. Otherwise returns the next result. */ function Fetch($resource=null) { if (is_null($resource)) { $this->SetError('Resource is a null object'); return false; } if (!is_resource($resource)) { $this->SetError('Resource ' . $resource . ' is not really a resource'); return false; } return stripslashes_array(mysql_fetch_assoc($resource)); } /** * Update * Runs an update query against the database. * * @param Query Update query to run * * @see Query * @see SetError * * @return boolean Returns true if the query is successful. Returns false if there is no query, no valid connection or the query failed. */ function Update($query='') { if (!$query) { $this->SetError('Query passed in is empty'); return false; } if (!$this->connection) { $this->SetError('No valid connection'); return false; } $result = $this->Query($query); if (!$result) { $this->SetError(mysql_error()); return false; } return true; } /** * NextID * Fetches the next id from the sequence passed in * * @param SequenceName Sequence Name to fetch the next id for. * * @see Query * * @return mixed Returns false if there is no sequence name or if it can't fetch the next id. Otherwise returns the next id */ function NextId($sequencename=false) { if (!$sequencename) return false; $query = "UPDATE " . $sequencename . " SET id=LAST_INSERT_ID(id+1)"; $result = $this->Query($query); if (!$result) return false; return mysql_insert_id(); } /** * FullText * Fulltext works out how to handle full text searches. Returns an sql statement to append to enable full text searching. * * @param fields Fields to search against * @param searchstring String to search for against the fields * @param splitsearchstring Boolean Whether to split the search string into separate match(against) calls. We need to do this for "contains all words" so it checks separately for each word. * * @return mixed Returns false if either fields or searchstring aren't present, otherwise returns a string to append to an sql statement. */ function FullText($fields=null, $searchstring=null, $splitsearchstring=false) { if (is_null($fields) or is_null($searchstring)) return false; if (is_array($fields)) { $fields = implode(',', $fields); } if (!$splitsearchstring) { $query = 'MATCH (' . $fields . ') AGAINST (\'' . addslashes($searchstring) . '\')'; return $query; } $searchlist = explode(' ', $searchstring); $subqueries = array(); foreach($searchlist as $search) { $subqueries[] = 'MATCH (' . $fields . ') AGAINST(\'' . addslashes(trim($search)) . '\')'; } $query = '(' . implode(' AND ', $subqueries) . ')'; return $query; } /** * AddLimit * This function creates the SQL to add a limit clause to an sql statement. * * @param offset Where to start fetching the results * @param numtofetch Number of results to fetch * * @return string The string to add to the end of the sql statement */ function AddLimit($offset=0, $numtofetch=0) { if ($offset < 0) $offset = 0; if ($numtofetch <= 0) $numtofetch = 10; $query = ' LIMIT ' . $offset . ', ' . $numtofetch; return $query; } /** * CountResult * Returns the number of rows returned for the resource passed in * * @param offset Where to start fetching the results * @param resource The result from calling Query * * @see Query * @see SetError * * @return int Number of rows from the result */ function CountResult($resource = null) { if (is_null($resource)) { $this->SetError('Resource is a null object'); return false; } if (!is_resource($resource)) { $this->SetError('Resource ' . $resource . ' is not really a resource'); return false; } $count = mysql_num_rows($resource); return $count; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -