Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CP_MySQL
- {
- //Member data
- var $connection = ""; //Holds the MySQL connection returned from mysql_connect()
- var $db = ""; //Holds the MySQL database selction from mysql_select_db()
- var $result = ""; //Holds the MySQL resource from mysql_query()
- var $query = ""; //Holds the last run query string.
- var $execution_time = ""; //Holds the amount of time it took to run the last query.
- function __construct($info = "")
- {
- if($info != "" && is_array($info)){
- $this->connect($info['host'], $info['user'],$info['pass']);
- $this->select_db($info['database']);
- }
- }
- //Member functions
- ///connect(string $server, string $username, string $password)
- ///\brief Connect to the MySQL database.
- ///@param $server - string - The server address.
- ///@param $username - string - The username to connect with.
- ///@param $password - string - The password to connect with.
- ///@return mysql_resource - Returns the resource from mysql_connect.
- function connect($server = "", $username = "", $password = "")
- {
- $this->connection = mysql_connect($server, $username, $password) or debug_print_backtrace();
- return $this->connection;
- }
- ///select_db(string $db)
- ///\brief Selects the database specfied by $db.
- ///@param $db - string - The database to select.
- ///@return mysql_resource - Returns the resource from mysql_select_db.
- function select_db($db = "")
- {
- $this->db = mysql_select_db($db, $this->connection) or debug_print_backtrace();
- return $this->db;
- }
- /**
- * query(string $query)
- * \brief Runs the specified query.
- * @param $query - string - The query to run.
- * @return mysql_resource - Returns the results of the query.
- */
- function query($query = "", $args = array())
- {
- $argt = array();
- preg_match_all("#((?<!%)|(?<=%%))%[bcdeufFosxX]#", $query, $argt, PREG_PATTERN_ORDER);
- //Strip out that useless $argt[1].
- $argt = $argt[0];
- if(count($argt) != count($args))
- {
- $cargt = count($argt);
- $cargs = count($args);
- echo "Error: Number of arguments ($cargs) not equal to number of expected arguments ($cargt).";
- print_r($argt);
- exit(0);
- }
- $i = 0;
- foreach($argt as $value)
- {
- switch($value)
- {
- case '%d':
- if(!is_numeric($args[$i]))
- exit("Error: Argument $i does not match specified type. Type: " /*. print_r($args,true) . "<br />Query: " . $query */);
- break;
- case '%s':
- break;
- default:
- print_r($argt);
- exit("Error: Invalid argument type '{$value}'." . $query);
- break;
- }
- $i++;
- }
- foreach($args as $value)
- $value = mysql_real_escape_string($value, $this->connection)or debug_print_backtrace();
- $this->query = vsprintf($query, $args);
- $this->result = mysql_query($this->query, $this->connection) or debug_print_backtrace();
- if(!$this->result)
- die('Invalid query: ' . $this->query . " - " . mysql_error());
- return $this->result;
- }
- ///get_result()
- ///\brief Returns the $results variable. The same return as query().
- ///@return mysql_resource - Returns the results of the last executed query.
- function get_result()
- {
- return $this->result;
- }
- ///set_result(mysql_resource $result)
- ///\brief Set the result from an outside variable.
- ///@param $result - mysql_resource - The result set to set.
- function set_result($result)
- {
- $this->result = $result;
- }
- ///fetch_array()
- ///\brief Returns the next row in the results.
- ///@return array - The fetched row.
- function fetch_array($result = 0)
- {
- if($result != 0)
- return mysql_fetch_array($result,MYSQL_ASSOC);
- return mysql_fetch_array($this->result,MYSQL_ASSOC);
- }
- ///num_rows()
- ///\brief Returns the number of rows in the results.
- ///@return int - Number of rows.
- function num_rows($result = 0)
- {
- if($result != 0)
- return mysql_num_rows($result)or debug_print_backtrace();
- return mysql_num_rows($this->result) or debug_print_backtrace();
- }
- ///data_seek(int $index)
- ///\brief Moves the internal pointer of the results to specified $index.
- ///@param $index - int - Index to move the pointer to. 0 >= $index > num_rows()
- function data_seek($index)
- {
- mysql_data_seek($this->result, $index);
- }
- ///escape_string(string $string)
- ///\brief Makes a string SQL-safe
- ///@param $string - string - The string to be made SQL safe
- ///@return string - The SQL safe string
- function escape_string($string)
- {
- return mysql_real_escape_string($string);
- }
- function free_result($result)
- {
- mysql_free_result($result);
- }
- function free($result){
- mysql_free_result($result);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment