<?
///////////////////////////////////////////
// Helper classes
///////////////////////////////////////////
class MAL extends DAL
{
protected $host = 'localhost';
protected $user = 'root';
protected $password = '';
protected $database = 'blog';
protected $link=null;
public $row_count = -1;
public $hasError = false;
public $errorMsg = '';
function __construct($host='',$user='',$password='-1',$database='')
{
/*
* instead of using app specific logic here, should be used MYMAL
* to be extended by models like PostModel
*/
global $papa_config;
//$papa_config->database->host,$papa_config->database->username,$papa_config->database->password,$papa_config->database->dbname
if(empty($host)){
$host = $papa_config['database']['host'];
}
if(empty($user)){
$user = $papa_config['database']['username'];
}
if($password=='-1'){
$password = $papa_config['database']['password'];
}
if(empty($database)){
$database = $papa_config['database']['dbname'];
}
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
//echo "$host,$password,$database,$user";
$this->link = mysql_connect($this->host,$this->user,$this->password);
echo(mysql_error($this->link));
//echo 'user = '.$papa_config->database->username.'<hr>';
mysql_select_db($this->database,$this->link);
//die($database);
mysql_query("SET character_set_results=utf8",$this->link);
$this->row_count = -1;
}
function execute($sql, array $params = array())
{
if( count($params) > 0 ){
$sql = vsprintf( $sql, $params );
}
//$this->link = mysql_connect($this->host,$this->user,$this->password);
mysql_select_db($this->database);
//mysql_select_db($this->database,$this->link);
//echo($sql);
mysql_select_db($this->database,$this->link);
//mysql_query("SET character_set_results=utf8",$this->link);
mysql_query("SET character_set_results=utf8");
mysql_set_charset('utf8');
//die($sql);
$res = mysql_query($sql);
@$this->row_count = mysql_num_rows($res);
@$this->hasError = mysql_errno($this->link);
if(empty($this->hasError)){
$this->hasError =false;
}else{
$this->hasError =true;
$this->errorMsg = mysql_error($this->link);
echo(mysql_error($this->link));
}
//var_dump($this->hasError);
//echo mysql_error($this->link);
//die('hre='.$this->row_count);
return $res;
}
public function getRowCount(){
return $this->row_count;
}
function get_table_columns($table){}
function get_connection()
{
return $this->link;
}
/**
* Checks if the value is unique in the user table
*
* @param $col The column in the user tabls
* @param $val The value to be checked
*/
public function isUnique( $col, $val, $table ){
$col = mysql_real_escape_string($col);
$val = mysql_real_escape_string($val);
$table = mysql_real_escape_string($table);
$sql = "select * from $table where $col='$val';";
$res = $this->execute($sql);
if($this->row_count>0){
return false;
}
return true;
}
//TODO simple prepared statement
/*
function preparedStatement(){}
//*/
}
?>