Database Class
LEUNG
Database Class 有 95 則迴響
<?php class Database{ protected $host; protected $user; protected $pass; protected $database; protected $port; function Database($host, $user, $pass, $database, $port){ $this->host = $host; $this->user = $user; $this->pass = $pass; $this->database = $database; $this->port = $port; } /** Connection Funtion **/ function conn(){ $this->conn = mysqli_connect($this->host. ":".$this->port, $this->user, $this->pass)or die("Invalid database"); mysqli_select_db($this->conn, $this->database) or die("Can not connect database"); mysqli_query($this->conn, "SET NAMES 'utf8'"); date_default_timezone_set('Hongkong'); return "Connect database succeed"; } function close() { mysqli_close($this->conn); } function error() { return mysqli_error($this->conn); } /** Execution Query Function **/ function query($query) { $this->query = $query; return $this->result = mysqli_query($this->conn, $this->query) or die(error()); } function insert_id() { return mysqli_insert_id($this->conn); } function num_rows() { return mysqli_num_rows($this->result); } function free_result() { mysqli_free_result($this->result) ; } function fetch_array($mode = MYSQLI_ASSOC) { return mysqli_fetch_array($this->result, $mode); } function affected_rows() { return mysqli_affected_rows($this->conn); } /** Function **/ function escape_string($string) { if (get_magic_quotes_gpc()) { $string = stripslashes($string); } if (!is_numeric($string)) { $string = mysqli_real_escape_string($this->conn, $string); } return trim($string); } /* function insert_escape_string($string) { return $this->real_escape_string($string, true); } */ function uniqid($length = 3){ $array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", 0,1,2,3,5,6,7,8,9); $key = ""; $size = sizeof($array)-1; for($i = $length; $i > 0;$i--) { $n = rand(0, $size); $key.= $array[$n]; } $uniqid = uniqid(); $uniqid .= md5($uniqid.$key); return $uniqid; } } ?>
Tutorial
$host = "127.0.0.1"; $user = "root"; $pass = ""; $db = "test"; $port = "3306"; $lang = "zh-tw"; $database = new Database($host, $user, $pass, $db, $port); $database->conn(); $sql = "SELECT * FROM test"; $result = $database->query($sql); $rows = $database->fetch_array($result); $database->free_result();