. */ /** * Provide an access point to MySQL databases. On initialisation of the class the authentication parameters are stored. Only when required, we do the actual login. * * @version 1.0 * @date 2.11.2009 * @author Wim van Ravesteijn * @license http://opensource.org/licenses/gpl-license.php GNU Public License */ class MySQL { private $conn; // MySQL link identifier private $host; // MySQL host private $user; // MySQL user private $pass; // MySQL password private $db; // MySQL database private $isConnected; // Are we connected already? private $res; // MySQL resource private $errorMsg; // Last error generated /** * Default constructor * * @param string host: hostname where the MySQL server runs * @param string user: username to be used to log in to MySQL * @param string pass: password to be used to log in to MySQL * @param string db: database to be used in MySQL */ public function __construct($host, $user, $pass, $db) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->db = $db; $this->isConnected = false; $this->errorMsg = ""; } /** * Default destructor */ public function __destruct() { } /** * Connect to the MySQL server (automatically called when database connection is needed) * * @return boolean true in case connecting is successful, false otherwise */ public function connect() { if( $this->isConnected ) { return true; } if( $this->conn = @mysql_pconnect($this->host, $this->user, $this->pass) ) { if( @mysql_select_db($this->db, $this->conn) ) { $is_connected = true; @mysql_query("set names utf8;"); return true; }else { $this->errorMsg = "Failed connecting to database mysql://".$this->host."/".$this->db; return false; } }else { $this->errorMsg = "Failed connecting to MySQL mysql://".$this->host."/"; return false; } } /** * Execute a query * * @param string query: query to be executed * @return boolean true in case of success, false otherwise */ public function query($query) { if( !$this->connect() ) { return false; } $this->res = mysql_query($query, $this->conn); if( $this->res ) { return true; }else { $this->errorMsg = mysql_error($this->conn); return false; } } /** * Escapes special characters in a string for use in a SQL statement * * @param string text: string to be escaped * @return string escaped text */ public function escape($text) { if( !$this->connect() ) { return false; } return mysql_real_escape_string($text, $this->conn); } /** * Return number of rows in last result * * @return int number of rows in last result */ public function getNumRows() { return @mysql_num_rows($this->res); } /** * Fetch a result row as an associative array and a numeric array * * @return array containing next result row */ public function fetchArray() { return @mysql_fetch_array($this->res); } /** * Fetch a result row as an associative array * * @return array containing next result row */ public function fetchAssoc() { return @mysql_fetch_assoc($this->res); } /** * Get number of affected rows in previous MySQL operation * * @return int number of affected rows */ public function getAffected() { return @mysql_affected_rows($this->conn); } /** * Get the ID generated from the previous INSERT operation * * @return int the ID generated for an AUTO_INCREMENT column by the previous INSERT query */ public function getInsertId() { return @mysql_insert_id($this->conn); } /** * Move internal result pointer * * @param int rownr: the desired row number of the new result pointer * @return boolean true on success, false otherwise */ public function gotoRow($rownr) { return @mysql_data_seek($this->res, $rownr); } /** * Get the last error * * @return string last error message generated */ public function getErrorMsg() { return $this->errorMsg; } } ?>