<?php
class Library
{
	var $con;
	var $ret;
	
	function __construct()
	{
		

	$this->con=mysqli_connect('localhost','trafiqer_apis','(nUMN?LuawL%','trafiqer_apis') or die(mysqli_error());
	$this->dbname='trafiqer_apis';

	
	/*
	$this->con=mysqli_connect('localhost','root','','api') or die(mysqli_error());
	$this->dbname='api';
	*/
		
		
	return $this->con;
	}
	
	function generalquery($sql)
	{
		$ret=mysqli_query($this->con, $sql) or die($sql);
		return $ret;
	}
	
	
	function insert($table,$data)
	{
		$key=array();
		$val=array();
		foreach($data as $x=>$x_val)
		{
			$x_val=mysqli_real_escape_string($this->con,$x_val);
			$key[]=$x;
			$val[]=$x_val;
		}
		$key=implode(",",$key);
		$val="'".implode("','",$val)."'";
		$sql="insert into $table(".$key.") values(".$val.")";
		
		$ret=$this->generalquery($sql);
		return $ret;
	}
	
	
	function select_count($table,$condition)
	{
		$sql="select count(*) as c  from $table where $condition";
		$ret=$this->generalquery($sql);
		$res=mysqli_fetch_array($ret);
		$s=$res['c'];
		return $s;
	}
	
	
	function last_insert_id($table,$fld)
	{
		$sql="select max($fld) as max from $table";
		$ret=$this->generalquery($sql);
		$res=mysqli_fetch_array($ret);
		$s=$res['max'];
		return $s;
	}
	
	function singledata($fld1,$fld2,$val2,$table)
	{
	    $sql2 = "SELECT $fld1 FROM $table WHERE $fld2='$val2'";
		$query2 =$this->generalquery($sql2);
		 $arr2=mysqli_fetch_array($query2);
		 if(mysqli_num_rows($query2)>0) { return $arr2['0']; } else { return 0;  }
	 
	}
	
	

}
?>