Created
May 22, 2013 19:09
-
-
Save yangqi/5630089 to your computer and use it in GitHub Desktop.
PHP: MySQLi Wrapper Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * MySQLi Wrapper Class 0.1 | |
| * | |
| * @author qyang | |
| * | |
| */ | |
| class DBi | |
| { | |
| /** | |
| * @var string The resulttype of mysqli result, defualt is MYSQLI_ASSOC, use setResultType() to change. | |
| */ | |
| private $resultType = MYSQLI_ASSOC; | |
| /** | |
| * @var string Warning message | |
| */ | |
| public $message = ''; | |
| private $_db; | |
| private $_result; | |
| /** | |
| * Creates the MySQLi object for usage. | |
| * | |
| * @param array $config Required connection params. | |
| */ | |
| public function __construct($config) { | |
| $this->_db = new mysqli($config['host'], $config['user'], $config['pass'], $config['table']); | |
| if ( $this->_db == false ) | |
| { | |
| throw new Exception($this->_db->error, $this->_db->errno); | |
| } | |
| } | |
| /** | |
| * Set the mysqli_result resulttype | |
| * | |
| * @param string resulttype, default is MYSQLI_ASSOC | |
| */ | |
| public function setResultType($type) | |
| { | |
| $type = strtoupper($type); | |
| if( in_array($type, array('MYSQLI_NUM', 'MYSQLI_ASSOC', 'MYSQLI_BOTH') ) | |
| { | |
| $this->resultType = $type; | |
| return true; | |
| } | |
| else | |
| { | |
| $this->message = "Unknown resulttype {$type}."; | |
| $this->errno = 1; | |
| return false; | |
| } | |
| } | |
| /** | |
| * Check if table exists | |
| * | |
| * @param string table name | |
| * @return boolean | |
| */ | |
| public function table_exists($table) | |
| { | |
| $this->query("SHOW TABLES LIKE '{$table}"); | |
| if( $this->_result->num_rows == 1 ) | |
| { | |
| return true; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } | |
| /** | |
| * Get table checksum | |
| * | |
| * @param string table name | |
| * @return integer checksum of the table | |
| */ | |
| public function table_checksum($table) | |
| { | |
| $this->query("CHECKSUM TABLE {$table}"); | |
| $row = $this->_result->fetch_assoc(); | |
| return $row['Checksum']; | |
| } | |
| /** | |
| * Do the query | |
| * | |
| * @param string sql statement | |
| * @return null | |
| */ | |
| public function query($sql) | |
| { | |
| $this->sql = $this->_db->real_escape_string($sql); | |
| $this->_result = $this->_db->query($sql); | |
| if ( !$this->_result ) | |
| { | |
| throw new Exception( $this->_db->error, $this->_db->errno ); | |
| } | |
| } | |
| /** | |
| * Get the result | |
| * | |
| * @param none | |
| * @return array | |
| */ | |
| public function result() | |
| { | |
| /* grab all data */ | |
| $data = array(); | |
| while ( $row = $this->_result->fetch_array($this->resultType) ) | |
| { | |
| $data[] = $row; | |
| } | |
| $this->_result->free(); | |
| return $data; | |
| } | |
| /** | |
| * Returns the auto_increment insert id | |
| * This MUST come after an insert query. | |
| */ | |
| public function id() | |
| { | |
| return $this->_db->insert_id; | |
| } | |
| /** | |
| * destruct class | |
| */ | |
| public function __destruct() | |
| { | |
| $this->_db->close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment