<?PHP
require_once("config.db.php");
class XSQL
{
protected $host_name;
protected $host_username;
protected $host_password;
protected $database;
protected $link_db;
protected $result_insert;
protected function check_empty_var($name,$var){
if(empty($var))
{
echo nl2br("<b>Warning!!!</b>\n\$$name is empty in class XSQL");
exit;
}//end of if
}//end of function check_empty_var()
public function sort_data_insert($data,$type='field'){
$this->check_empty_var('data(sort_data_insert)',$data);
if(!is_string($data) && !is_array($data))
{
echo "\$data in function <u>sort_data_insert</u> must <strong>Array</strong> or <strong>String</strong><br/>";
exit;
}//end of if
if(strtolower($type)=='field') $char_str="`";
if(strtolower($type)=='value') $char_str="'";
$final_data=NULL;
is_string($data) ? $array_data=explode(',',$data) : $array_data=$data;
while(list($key,$val) = each($array_data))
{
($key != 0) ? $final_data .= ",$char_str".$val."$char_str" : $final_data .= "$char_str".$val."$char_str";
}//end of while
return $final_data;
}//end of function values_of_insert
public function sort_data_update($fields,$values){
$this->check_empty_var('fields(sort_data_update)',$fields);
$this->check_empty_var('values(sort_data_update)',$values);
if((!is_array($fields) && !is_string($fields)))
{
echo "\$fields in function <u>".__FUNCTION__."</u> must have a <strong>Array</strong> or <strong>String</strong><br/>";
exit;
}//end of if
if((!is_array($values) && !is_string($values)))
{
echo "\$values in function <u>".__FUNCTION__."</u> must have a <strong>Array</strong> or <strong>String</strong><br/>";
exit;
}//end of if
$final_data=NULL;
$array_fields=array();
$array_values=array();
is_string($fields) ? $array_fields = explode(',',$fields) : $array_fields = $fields;
is_string($values) ? $array_values = explode(',',$values) : $array_values = $values;
if(count($array_fields) != count($array_values))
{
echo "number of <strong><u>fields</u></strong> not equal with number
<strong><u>values</u></strong> in function<strong> ".__FUNCTION__." </strong>";
exit;
}//end of if
$lenfor=count($array_fields);
for($i=0;$i<$lenfor;$i++){
if(empty($array_fields[$i]))
{
echo "name <strong>fields</strong> contain a empty value
in function<strong> ".__FUNCTION__." </strong>";
exit;
}//end of if
$final_data .= "`".$array_fields[$i]."`='".$array_values[$i]."'";
if(($lenfor-1)!=$i) $final_data.=",";
}//end of for
return $final_data;
}//end of function sort_data_update()
protected function xsql_connect(){
global $host,$user,$pass,$db;
$this->host_name=$host;
$this->host_username=$user;
$this->host_password=$pass;
$this->database=$db;
$this->link_db=$connect_db=@mysql_connect($this->host_name,$this->host_username,$this->host_password);
if(!$connect_db)
{
echo "<h5>can`t connect to ".$this->host_name."</h5>please check serverName,userName,password";
exit;
}//end of if
//$this->xsql_connect_persian();
$this->xsql_select_db($this->database);
//$this->xsql_connect_persian();
}//end of function xsql_connect()
protected function xsql_connect_persian () {
mysql_query("set character set utf-8;");
mysql_query("set session collation connection='utf8-persian-ci';");
}//end of function xsql_connect_persian ()
protected function xsql_select_db($db){
$select_db=mysql_select_db($db);
if(!$select_db)
{
echo "<h5>can`t select database <u>$db</u></h5>";
exit;
}//end of if
}//end of function xsql_select_db()
protected function xsql_close(){
mysql_close();
}//end of function xsql_close();
public function xsql_mysql_info(){
$this->xsql_connect();
$mysql_info=mysql_info();
$this->xsql_close();
return $mysql_info;
}//end of function zsql_mysql_info()
public function xsql_error($num=false){
if($num === true)
{
$error = mysql_errno($this->link_db);
} else {
$error = mysql_error($this->link_db);
}//end of if
return $error;
}//end of function xsql_error()
public function xsql_query($query){
$this->check_empty_var('query',$query);
$this->xsql_connect();
$result_query=mysql_query($query);
if(!$result_query) return $this->xsql_error();
$this->result_insert=$result_query;
$this->xsql_close();
return $result_query;
}//end of function xsql_query()
public function xsql_affected_rows(){
$affected = mysql_affected_rows($this->link_db);
return $affected;
}//end of function xsql_affected_rows()
public function xsql_num_rows($result){
$this->check_empty_var('result(num_rows)',$result);
$number_rows=mysql_num_rows($result);
return $number_rows;
}//end of function xsql_num_rows()
public function xsql_name_fields($result){
$length_fields=mysql_num_fields($result);
$array_name_fields=array();
for($i=0;$i<$length_fields;$i++){
$array_name_fields[]=mysql_field_name($result,$i);
}//end of for
return $array_name_fields;
}//end of function xsql_num_fields()
public function xsql_fetch_array($result,$type=1){
$this->check_empty_var('result(fetch_array)',$result);
if($type!=1 && $type!=2 && $type!=3) $type=3;
$arrtype=array(1=>MYSQL_ASSOC,2=>MYSQL_NUM,3=>MYSQL_BOTH);
$return_data=mysql_fetch_array($result,$arrtype[$type]);
return $return_data;
}//end of function xsql_fetch_array()
public function xsql_fetch_assoc($result){
$this->check_empty_var('result(fetch_assoc)',$result);
$return_data=mysql_fetch_assoc($result);
return $return_data;
}//end of function xsql_fetch_assoc()
public function xsql_fetch_row($result){
$this->check_empty_var('result(fetch_row)',$result);
$return_data=mysql_fetch_row($result);
return $return_data;
}//end of function xsql_fetch_assoc()
public function xsql_result($result,$row=0){
$this->check_empty_var('result(result)',$result);
$return_data=mysql_result($result,$row);
return $return_data;
}//end of function xsql_result()
public function xsql_last_insert_id(){
$return_id=mysql_insert_id($this->link_db);
return $return_id;
}//end of function xsql_last_insert_id()
public function xsql_real_escape_string($string){
//$this->check_empty_var('string(real_escape_string)',$string);
$this->xsql_connect();
$escped=mysql_real_escape_string($string);
$this->xsql_close();
return $escped;
}//end of function xsql_real_escape_string()
}//end of class XSQL
?>