<?php
function get_field_name($table,$fld,$where,$target)
{
	$ret = "";
	$sql = "SELECT $fld FROM $table WHERE $where = '$target'";
	
	$res = mysql_query($sql);
	$numrows=mysql_num_rows($res);
	while($row = mysql_fetch_object($res))
	{
		$ret = $row->$fld;
	}
	return $ret;
}
function get_site_name()
{
	$ret = "";
$sql = "SELECT siteurl FROM admin";
	
	$res = mysql_query($sql);
	$numrows=mysql_num_rows($res);
	while($row = mysql_fetch_object($res))
	{
		$ret = $row->siteurl;
	}
	return $ret;
}

 

function tep_not_null($value) {
    if (is_array($value)) {
      if (sizeof($value) > 0) {
        return true;
      } else {
        return false;
      }
    } else {
      if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) {
        return true;
      } else {
        return false;
      }
    }
  }

  function tep_db_error($query, $errno, $error) {
    die('<font color="#000000"><b>' . $errno . ' - ' . $error . '<br><br>' . $query . '<br><br><small><font color="#ff0000">[TEP STOP]</font></small><br><br></b></font>');
  }

  function tep_db_query($query) {
  
    global  $logger;
    $result = mysql_query($query ) or tep_db_error($query, mysql_errno(), mysql_error());
    return $result;
  }

  function tep_db_perform($table, $data, $action = 'insert', $parameters = '') {
    reset($data);
    if ($action == 'insert') {
      $query = 'insert into ' . $table . ' (';
      while (list($columns, ) = each($data)) {
        $query .= $columns . ', ';
      }
	 
      $query = substr($query, 0, -2) . ') values (';
      reset($data);
      while (list(, $value) = each($data)) {
        switch ((string)$value) {
          case 'now()':
            $query .= 'now(), ';
            break;
          case 'null':
            $query .= 'null, ';
            break;
          default:
            $query .= '\'' . tep_db_input($value) . '\', ';
            break;
        }
      }
      $query = substr($query, 0, -2) . ')';
    } 
	elseif ($action == 'update') {
      $query = 'update ' . $table . ' set ';
      while (list($columns, $value) = each($data)) {
        switch ((string)$value) {
          case 'now()':
            $query .= $columns . ' = now(), ';
            break;
          case 'null':
            $query .= $columns .= ' = null, ';
            break;
          default:
            $query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
            break;
        }
      }
   $query = substr($query, 0, -2) . ' where ' . $parameters;
	
 
    }
//  $query."<br>";
    return tep_db_query($query);
 
  }

  function tep_db_fetch_array($db_query) {
    return mysql_fetch_array($db_query, MYSQL_ASSOC);
  }
 function tep_db_fetch_object($db_query) {
    return mysql_fetch_object($db_query, MYSQL_ASSOC);
  }
  
  function tep_db_result($result, $row, $field = '') {
    return mysql_result($result, $row, $field);
  }

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

  function tep_db_data_seek($db_query, $row_number) {
    return mysql_data_seek($db_query, $row_number);
  }

  function tep_db_insert_id() {
    return mysql_insert_id();
  }

  function tep_db_free_result($db_query) {
    return mysql_free_result($db_query);
  }
/*function tep_db_fetch_object($db_query) {
    return mysql_fetch_object($db_query);
  }*/
  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(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;
    }
  }
  
?>