Skip to content

Instantly share code, notes, and snippets.

@shinyzhu
Created August 27, 2010 02:25
Show Gist options
  • Save shinyzhu/552657 to your computer and use it in GitHub Desktop.
Save shinyzhu/552657 to your computer and use it in GitHub Desktop.
DAL for SAE
<?php
/*
Data Access Layer for FansWall(http://fanswall.sinaapp.com)
Author: Shiny Zhu(http://t.sina.com.cn/shinyzhu)
*/
class DAL{
private $server, $username, $password, $db;
private $link;
private $result;
public function __construct($server, $username, $password, $db){
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connect();
}
function __destruct(){
if(!$this->result) mysql_free_result($this->result);
mysql_close($this->link);
}
private function connect(){
$this->link = mysql_connect($this->server, $this->username, $this->password) or die("Can NOT connect: " . mysql_error());
mysql_select_db($this->db, $this->link) or die("Can NOT select the database '" . $this->db . "'.");
}
public function query($sql){
if($this->result = mysql_query($sql, $this->link) or die("Query failed: " . mysql_error())){
return $this->result;
}else{
return false;
}
}
public function fetch_array($result){
return mysql_fetch_array($result);
}
public function fetch_first($sql){
return mysql_fetch_array($this->query($sql));
}
public function num_rows($result){
return mysql_num_rows($result);
}
public function update($sql){
return $this->query($sql);
}
public function insert($sql){
return $this->query($sql);
}
function get_last_id(){
return mysql_insert_id();
}
public function __toString(){
return "Database '". $this->db . "' from mysql server hosted at '" . $this->server . "'.";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment