<?php
 $connected = false;
 function c()
  {
      global $db_host, $db_login, $db_pswd,$db_name;

      $db = @mysqli_connect($db_host, $db_login, $db_pswd,$db_name) or die("COULD NOT CONNCECT");
	  //echo "CONNECTED";
	  
      $connected = true;
	 
      return $db;
  }

 function q($q_str)
 {
    global $db_name, $connected;
    if(!$connected)
    {
    	$db=c();
    }

   //mysqli_select_db($db_name);
    //$r = mysqli($db_name, $q_str);
	$r=mysqli_query($db,$q_str);
	d($db_name);
    return $r;
 }

 function d($db)
 {
    @mysqli_close($db);
 }

 function e($r)
 {
  if(@mysqli_numrows($r))
   return 0;
  else return 1;
 }

 function f($r){
  return @mysqli_fetch_array($r);
 }

 function nr($r){
  return @mysqli_num_rows($r);
 }
 function iid(){
  return @mysqli_insert_id();
 }
 
 
 function sql_select($table,$fields,$arg,$debug)
{
  
  if($arg<>"")
  {
    $str= q('SELECT '.$fields.' FROM '.$table.' WHERE '.$arg);
	 
  
  }
  else
  {
    $str=q('SELECT '.$fields. 'FROM '.$table);
  
  }
  
  if($debug==1)
  {
    echo '<br>Error:'.mysqli_error().'At:'.__LINE__.__FILE__.'.</br>';
	return false;
  }
return $str;
}


function sql_insert($table, $arg, $debug = FALSE)
{
 

   if(is_array($arg))
		{
			foreach($arg as $k => $v)
			{
				$keyList .= ($keyList ? ",`{$k}`" : "`{$k}`");
				$valList .= ($valList ? ",'{$v}'" : "'{$v}'");
			}
			$query = "INSERT INTO `{$table}` ({$keyList}) VALUES ({$valList})";
		}
		else
		{
			$query = 'INSERT INTO '."{$table} VALUES ({$arg})";
		}
 
 
 if ($result = q($query))
  {
	$tmp = mysqli_insert_id();
	return $tmp;
	}
	 else
		 {
			if($debug==1)
  			 {
    				 echo "<br>Error:".mysqli_error()." At:".__LINE__.__FILE__;
	 				return false;
  			 }	
		}
		
		
 	
}



function sql_update($table, $arg, $debug = FALSE)
{
   
   
   
   if ($result =q('UPDATE '.$table.' SET '.$arg)) {
			$result = mysqli_affected_rows();
			return $result;
		} 


if($debug==1)
   {
     echo "<br>Error:".mysqli_error()." At:".__LINE__.__FILE__;
	 return false;
   }
}

function sql_delete($table, $arg = '', $debug = FALSE)
{


  if (!$arg) 
  {
			if ($result =q('DELETE FROM '.$table)) 
			{
				return $result;
			} else 
			{
				//$this->dbError("db_Delete ($arg)");
				return FALSE;
			}
		} 
		
 else 
	{
		
		if ($result = q('DELETE FROM '.$table.' WHERE '.$arg))
		 {
				$tmp = mysqli_affected_rows();
				return $tmp;
			} 
			else 
			{
				//$this->dbError('db_Delete ('.$arg.')');
				return FALSE;
			}
		}


 if($debug==1)
   {
     echo "<br>Error:".mysqli_error()." At:".__LINE__.__FILE__;
	 return false;
   }
}

?>
