Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /** A class for handling SQL */
- class LiteSQL {
- private $prepared_statements = array();
- private $sql = null;
- /** Initializes the SQL Connection */
- public function __construct(){
- if(!$this->OpenSQL()){
- exit();
- }
- }
- /** Closes the SQL connection and destroys the object */
- public function __destruct(){
- $this->CloseSQL();
- }
- /**
- * Escapes a string for sql injection
- * @param String $str
- */
- public function escapeString($str){
- return $this->sql->real_escape_string(strip_tags($str));
- }
- /**
- * Executes prepared statement
- * @param String $prepareTitle The prepared statement to execute
- * @param Boolean $return If set to true, will return a ResultSet
- * @return Object The value of the execution
- */
- public function executePreparedStatement($prepareTitle, $return = false, $binds = array()){
- $result = null;
- if(sizeof($binds) > 0){
- $tmp = array();
- foreach($binds as $key => $value) $tmp[$key] = &$binds[$key];
- call_user_func_array(array($this->prepared_statements[$prepareTitle], 'bind_param'), $tmp);
- $ret = $this->prepared_statements[$prepareTitle]->execute();
- if($return){
- $ret = $this->prepared_statements[$prepareTitle]->get_result();
- }
- $this->removePreparedStatement($prepareTitle);
- return $ret;
- }
- /**
- * Gets a Prepared Statement based on it's title
- * @param String $prepareTitle The key for the statement
- * @return PreparedStatement The Prepared Statement
- */
- public function retrievePreparedStatement($prepareTitle){
- return $this->prepared_statements[$prepareTitle];
- }
- /**
- * Create new prepared statement in the system
- * @param String $prepareTitle The title for the statement
- * @param String $prepareBody The prepared statement
- * @return String|Object Thre prepareTitle passed
- */
- public function prepareStatement($prepareTitle, $prepareBody){
- $this->prepared_statements[$prepareTitle] = $this->sql->prepare($prepareBody);
- return $prepareTitle;
- }
- /**
- * Removes a Prepared Statement from the system
- * @param String $prepareTitle The key for the statement
- */
- public function removePreparedStatement($prepareTitle){
- $this->prepared_statements[$prepareTitle]->close();
- unset($this->prepared_statements[$prepareTitle]);
- }
- /**
- * Executes a query in sql
- * Note: Make sure you use ThrowLite::escapeString on all user values
- * @param String $query
- * @param Boolean $return If set to true, will return a ResultSet
- */
- public function executeSql($query, $return = false, $binds = array()){
- $id = $this->prepareStatement(sizeof($this->prepared_statements), $query);
- $ret = $this->executePreparedStatement($id, $return, $binds);
- if($this->sql->errno){
- echo new UserSystemException("Error while executing SQL", $this->sql->error);
- exit;
- }
- return $ret;
- }
- /**
- * Creates the default tables in the databases
- * @return Boolean true if completed otherwise false
- */
- public function CreateDefaultTables(){
- return (
- $this->executeSql(SQL_CreateTable_USERS)
- && $this->executeSql(SQL_CreateTable_MAIL)
- && $this->executeSql(SQL_CreateTable_NEWS)
- && $this->executeSql(SQL_CreateTable_LOGS)
- && $this->executeSql(SQL_CreateTable_IMAGES)
- && $this->executeSql(SQL_CreateTable_COMMENTS)
- && $this->executeSql(SQL_CreateTable_COMMENTTHREADS)
- );
- }
- /**
- * Closes current SQL connection
- */
- public function CloseSQL(){
- if($this->sql != null) { $this->sql->close(); $this->sql = null; }
- }
- /**
- * Open SQL connection
- */
- public function OpenSQL(){
- $this->CloseSQL();
- $this->sql = @new mysqli(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD, SQL_DATABASE, SQL_PORT);
- if($this->sql->connect_errno){
- echo new TLSQLException("Error while connecting to MySql", "MYSQL ERROR: There was an error while trying to connect to sql server ".SQL_USERNAME."@".SQL_SERVER." using password '".((SQL_PASSWORD != "") ? "[YES]" : "[NO]")."'<br /><br />".mysqli_connect_error());
- return false;
- }
- return true;
- }
- /**
- * Returns the auto generated id used in the last query
- * @return The last ID inserted using auto increment column
- */
- public function GetLastInsertID(){
- return $this->sql->insert_id;
- }
- /**
- * Gets the current SQLI Object
- * @return mysqli The Current SQL Object
- */
- public function GetSQL(){
- return $this->sql;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement