Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Class DBCommon {
- private $_host = 'localhost';
- private $_user = 'root';
- private $_pass = '';
- private $_db = 'hotel';
- private $_link = null;
- private $_link_db = null;
- protected $_tableName = null;
- public function __construct() {
- $this->connect();
- }
- public function getTableName(){
- return $this->_tableName;
- }
- public function setTableName($name){
- $this->_tableName = $name;
- }
- public function getLink()
- {
- return $this->_link;
- }
- /**
- * Conecta ao banco de dados
- *
- * @return bool
- */
- public function connect() {
- if($this->_link == null && $this->_link == false) {
- $this->_link = mysql_connect($this->_host, $this->_user, $this->_pass);
- $this->_link_db = mysql_select_db($this->_db);
- return true;
- } else {
- if(!is_resource($this->_link)) {
- $this->_link = mysql_connect($this->_host, $this->_user, $this->_pass);
- $this->_link_db = mysql_select_db($this->_db);
- return true;
- }
- }
- }
- /**
- * Fecha uma conexao ao banco de dados
- *
- * @return bool
- */
- public function close(){
- if($this->_link == null) {
- return true;
- } else {
- if(is_resource($this->_link)) {
- return mysql_close($this->_link);
- } else {
- return true;
- }
- }
- }
- /**
- * Insere um dado na tabela
- *
- * @param string $table tabela que irá receber os dados
- * @param array $data dados que serão inseridos, onde a chave é o campo e o valor o dado a ser inserido
- * @return bool
- */
- static public function insert($table = '', $data) {
- if(!is_array($data) || $table == '') {
- return false;
- }
- if(count($data)==0) {
- return false;
- }
- $fields = array();
- $values = array();
- foreach ($data as $field=>$value) {
- $fields[] = $field;
- if($value == 'NOW()') {
- $values[] = $value;
- } else {
- $values[] = "'" . $value . "'";
- }
- }
- $this->connect();
- $strFields = implode(',', $fields);
- $strValues = implode(',', $values);
- $sql = 'INSERT INTO ' . $table . ' (' . $strFields . ') VALUES (' . $strValues . ');';
- $resource = mysql_query($sql, $this->_link);
- $id = mysql_insert_id($this->getLink());
- $this->close();
- return $id;
- }
- static public function query($sql)
- {
- $link = $this->connect();
- $rs = mysql_query($sql, $link);
- $this->close();
- return $rs;
- }
- static public function delete($table, $where)
- {
- $link = $this->connect();
- $sql = 'DELETE FROM ' . $table . ' ' . $where;
- $rs = mysql_query($sql, $link);
- $this->close();
- return $rs;
- }
- static public function fetch($sql, $type=1)
- {
- $types = array(
- 1 => MYSQL_ASSOC,
- 2 => MYSQL_NUM,
- 3 => MYSQL_BOTH
- );
- if($types[$type]) {
- $resultType = $types[$type];
- } else {
- $resultType = $types[3];
- }
- $obj = new DBCommon();
- $obj->connect();
- $link = $obj->getLink();
- $rs = mysql_query($sql, $link);
- $data = array();
- if($rs) {
- if(mysql_num_rows($rs)) {
- while($row = mysql_fetch_array($rs, $resultType)) {
- $data[] = $row;
- }
- }
- } else {
- return false;
- }
- return $data;
- }
- }
Add Comment
Please, Sign In to add comment