Created
July 23, 2014 02:35
-
-
Save phamquocbuu/8d272d4ac1d452422b8a to your computer and use it in GitHub Desktop.
Custom class for Database connecting and executing sql query
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 | |
class CosDb { | |
private $DB_HOST; | |
private $DB_PORT; | |
private $DB_NAME; | |
private $DB_USER; | |
private $DB_PASS; | |
private $link; | |
public function __construct($user, $pass, $db_name, $host = 'localhost', $port = null) { | |
$this->setInfo($user, $pass, $db_name, $host, $port); | |
$this->link = mysqli_connect($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME, $this->DB_PORT); | |
if (!$this->link || $this->link->connect_errno) { | |
printf("Connect failed: %s\n", $mysqli->connect_error); | |
exit(); | |
} | |
} | |
private function setInfo($user, $pass, $db_name, $host, $port) | |
{ | |
$this->DB_HOST = $host; | |
$this->DB_PORT = is_null( $port ) ? ini_get("mysqli.default_port") : $port; | |
$this->DB_NAME = $db_name; | |
$this->DB_USER = $user; | |
$this->DB_PASS = $pass; | |
} | |
public function query($sql) | |
{ | |
$this->link->query("SET NAMES utf8;"); | |
$result = $this->link->query($sql); | |
return $result; | |
} | |
public function close() | |
{ | |
$this->link->close(); | |
} | |
public function real_escape_string($value='') | |
{ | |
return $this->link->real_escape_string($value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment