Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class database {
- public $lastInsertId;
- private $host;
- private $username;
- private $password;
- private $database;
- private $connection;
- private $legacyConnection;
- public function __construct($host, $username, $password, $database) {
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);
- if($this->connection->connect_errno > 0) {
- die('Unable to connect with Mysql database...');
- }
- $this->legacyConnection = mysql_connect($this->host, $this->username, $this->password);
- }
- public function __destruct() {
- $this->connection->close();
- if($this->legacyConnection) {
- @mysql_close($this->legacyConnection);
- }
- }
- /** $database->query
- * Works like runQuery except it'll return an array of objects or a single object if only one row is collected. If $forceArrayReturn itll always return an array
- */
- public function query($queryString, $forceArrayReturn = false) {
- $result = $this->connection->query($queryString);
- //bail if query fail. return true if bool was returned
- if(!$result) {
- return false;
- }elseif($result === true) {
- return true;
- }
- //collect rows
- while($row = $result->fetch_assoc()){
- $return[] = $row;
- }
- if(count(@$return) === 1) {
- $obj = new stdClass();
- foreach($return[0] as $key => $value) {
- $obj->$key = $value;
- }
- if($forceArrayReturn) {
- return array($obj);
- }
- return $obj;
- }elseif(sizeof(@$return) >= 2){
- foreach($return as $row) {
- $obj = new stdClass();
- foreach($row as $key => $value) {
- $obj->$key = $value;
- }
- $returnRows[] = $obj;
- }
- return $returnRows;
- }
- }
- public function runQuery($query) {
- mysql_select_db($this->database, $this->legacyConnection);
- $result = mysql_query($query);
- return $result;
- }
- public function dbPrepare($string, $case = "default") {
- $string = mysql_real_escape_string($string);
- if (empty($string)) {
- $string = "NULL";
- } else {
- switch ($case) {
- case "upper":
- $string = strtoupper($string);
- break;
- case "lower":
- $string = strtolower($string);
- break;
- case "ucfirst":
- $string = strtolower($string);
- $string = ucfirst($string);
- break;
- case "lcfirst":
- $string = lcfirst($string);
- break;
- case "ucwords":
- $string = strtolower($string);
- $string = ucwords($string);
- break;
- default:
- break;
- }
- $string = "'" . $string . "'";
- }
- return $string;
- }
- function checkEmpty($string) {
- $string = mysql_real_escape_string($string);
- if (empty($string)) {
- $string = "'%'";
- return $string;
- } else {
- $string = "'$string'";
- return $string;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement