<?php

/******************************** TO CONNECT DATABASE  *******************************/

/* Required parameter in following sequence
	1) Server Name
	2) User Name to connect database
	3) Password to connect databse
	4) database name

	if you connect to the database $link will be your connection variable */

  function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE) {
    global $link;

	if (USE_PCONNECT == 'true') {
      $link = mysql_pconnect($server, $username, $password) or die("Can not connect to database");
    } else {
      $link = mysql_connect($server, $username, $password) or die("Can not connect to database");
    }

    if ($link) {
		mysql_select_db($database) or die("Can not select the database");
	} 

    return $link;
  }
  
   
   function tep_db_connect_comm($server = DB_SERVER_COMMON, $username = DB_SERVER_USERNAME_COMMON, $password = DB_SERVER_PASSWORD_COMMON, $database = DB_DATABASE_COMMON) {
    global $link;

	if (USE_PCONNECT == 'true') {
      $link = mysql_pconnect($server, $username, $password);
    } else {
      $link = mysql_connect($server, $username, $password);
    }

    if ($link) mysql_select_db($database);

    return $link;
  }  

/******************************** TO CLOSE DATABASE CONNECTION *******************************/

/* required  $link : connection variable to pass as parameter  */

  function tep_db_close($link) {
     return mysql_close($link);
  }

/******************************** TO EXECUTE 'select' QUERY *******************************/

/* required two parameter
	1) $query : your sql 'select' query
	2) $link : you connection variable  
	
		This will give your recordset in variable $result	
*/


  function tep_db_query($query, $link) {
     $result = mysql_query($query, $link) or tep_db_error($query, mysql_errno(), mysql_error());

    return $result;
  }


/******************************** TO FETCH ARRAY OF DATA FROM DATABASE *******************************/

/* required $db_query : your recordset variable */

  function tep_db_fetch_array($db_query) {  
    return mysql_fetch_array($db_query, MYSQL_ASSOC);
  }

/******************************** TO DISPLAY ERROR MESSAGE *******************************/

/* required three parameters
	1) $query : your SQL query in which error occured
	2) $errno : error number of your SQL query
	3) $error : description of the error message */

  function tep_db_error($query, $errno, $error) { 
  	echo "Query : ".$query."<br><br>";
	echo "Error No : ".$errno."<br><br>";
	echo "Error : ".$error."<br><br>";
	exit;	
  }


/******************************** TO GET NUMBER OF ROWS *******************************/

/* required $query : your recordset variable */

  function tep_db_num_rows($db_query) {
    return mysql_num_rows($db_query);
  }


/******************************** TO FREE YOUR RECORDSET *******************************/

/* just pass your recordset variable */

  function tep_db_free_result($db_query) {
    return mysql_free_result($db_query);
  }

/******************************** TO FETCH FIELD NAME OF THE RECORD SET *******************************/

/* just pass your recordset variable */

  function tep_db_fetch_fields($db_query) {
    return mysql_fetch_field($db_query);
  }

  function tep_db_output($string) {
    return htmlspecialchars($string);
  }

  function tep_db_input($string) {
    return addslashes($string);
  }

  function tep_db_prepare_input($string) {
    if (is_string($string)) {
      return trim(tep_sanitize_string(stripslashes($string)));
    } elseif (is_array($string)) {
      reset($string);
      while (list($key, $value) = each($string)) {
        $string[$key] = tep_db_prepare_input($value);
      }
      return $string;
    } else {
      return $string;
    }
  }
//===========================================================
$conn="";

function init(){
 $str_servername=DB_SERVER;
 $str_username=DB_SERVER_USERNAME;
 $str_password=DB_SERVER_PASSWORD;
 $str_database_name=DB_DATABASE;
 
        $conn=tep_db_connect();
        return $conn;
}


function tf_select($str_table_name, $str_field, $str_where="",$str_likecolumn="",$str_likecondition="",$int_start=0,$int_end=0,$str_orderby="")
{
 $str_query="";
 $result="";
 	if($str_orderby!="")
 	{
		 if($str_likecolumn=="" && $str_where!=""){
		 	if($int_start=="0" && $int_end=="0"){
				$str_query="select distinct ".implode(",",$str_field)." from $str_table_name where $str_where order by $str_orderby";
			}else{
				$str_query="select distinct ".implode(",",$str_field)." from $str_table_name where $str_where order by $str_orderby LIMIT $int_start,$int_end";	
			}
		 }else if($str_where=="" && $str_likecolumn==""){
		  $str_query="select distinct ".implode(",",$str_field)." from $str_table_name  order by $str_orderby";
		 }else{
			if($int_start==0 && $int_end==0){
					$str_query="select ".implode(",",$str_field)." from $str_table_name where $str_likecolumn like '$str_likecondition%'  order by $str_orderby";
			}else{
				
					$str_query="select ".implode(",",$str_field)." from $str_table_name where $str_likecolumn like '$str_likecondition%' order by $str_orderby LIMIT $int_start,$int_end";	
			}
		 }
	}
	else
	{
		 if($str_likecolumn=="" && $str_where!=""){
				$str_query="select distinct ".implode(",",$str_field)." from $str_table_name where $str_where";
		 }else if($str_where=="" && $str_likecolumn==""){
		  $str_query="select distinct ".implode(",",$str_field)." from $str_table_name";
		 }else{
			if($int_start==0 & $int_end==0){
					$str_query="select ".implode(",",$str_field)." from $str_table_name where $str_likecolumn like '$str_likecondition%'";
			}else{
					$str_query="select ".implode(",",$str_field)." from $str_table_name where $str_likecolumn like '$str_likecondition%' LIMIT $int_start,$int_end";	
			}
		 }
	
	}
 	//str_replace("n12br","-",$str_query);
	//echo $str_query;
 	$result=tep_db_query($str_query,init());
	////mysql_close($conn);
	return $result;
}

function tf_insert($str_table_name, $str_field, $obj_values)
{
$val_part="";
$recresult="";
    $str_query = "insert into `$str_table_name` (";
    $int_count = 0;
    while ( $int_count < count($str_field) )
    {
        $str_query = $str_query .$str_field[$int_count];
        $val_part = $val_part .$obj_values[$int_count];
        if ( ($int_count + 1) != count($str_field) )
        {
            $val_part = $val_part . ", ";
            $str_query = $str_query. ", ";
        }
        $int_count++;
    }
    $str_query = $str_query.") values(".$val_part.")";
	echo $str_query;
//	exit();
	$recresult=tep_db_query($str_query,init());
/*
	if(!$recresult)
	{
		$str_error_message = 'Database Error - '.mysql_errno().': '.mysql_error().' in Update()';
		error_handling('', $str_error_message);
		return 0;
	}
*/	
	//mysql_close($conn);
    return mysql_affected_rows();
}

/************************************************************************************** 
* Author: Ranjana Prajapati
* Methods: 
*         tf_update 
* Description
		 Update the record in database
**************************************************************************************/ 
function tf_update($str_table_name, $str_fields, $obj_values,$str_columnname,$obj_condition)
{
$recresult="";
$columns ="";
$str_query ="";
    $str_query .= "UPDATE `$str_table_name` SET ";
    $int_count = 1;
	foreach ($str_fields as $int_key=>$int_value) {
        if ($int_count != count($str_fields) )
        {
			$str_query .= $str_fields[$int_key];
			$str_query .= "=".$obj_values[$int_key].",";
        }
		else
		{
			$str_query .= $str_fields[$int_key];
			$str_query .= "=".$obj_values[$int_key];		
		}
		$int_count++;
	}

	if(count($str_columnname)>0){
		$str_query .= " WHERE " ;
	
		$int_count = 1;
		foreach ($str_columnname as $int_key=>$int_value) {
			if ($int_count != count($str_columnname) )
			{
				$str_query .= $str_columnname[$int_key];
				$str_query .= "=".$obj_condition[$int_key]." and ";
			}
			else
			{
				$str_query .= $str_columnname[$int_key];
				$str_query .= "=".$obj_condition[$int_key];		
			}
			$int_count++;
		}
	}
	echo $str_query;
	$recresult=tep_db_query($str_query,init());
//  Error handling
	if (!$recresult)
	{
		//$str_error_message = 'Database Error - '.mysql_errno().': '.mysql_error().' in Update()';
		$str_error_message = 'Database Error - '.mysql_errno().': '.mysql_error().' in Update()';
		error_handling('', $str_error_message);
		//mysql_close($conn);
		return 0;
	}else{
		//mysql_close($conn);
		return mysql_affected_rows();
	}
}


/* GET MAX INSERT ID*/
function getmaxid()
{
	return mysql_insert_id(init());
}

?>