<?php
class binnDbSql 
{
    var $mHost     = '';
    var $mDatabase = '';
    var $mUser     = '';
    var $mPassword = '';
    
    var $mErrno    = 0;
    var $mError    = '';
    
    var $mShowErrors = false;
    var $mLinkId   = 0;
    var $mQueryId  = 0;

    function binnDbSql($Database = '', $Host = '', $User = '', $Password = '') 
    {
        if (0 != func_num_args())
        {
            if ('' != $Database)
                $this->mDatabase = $Database;
            if ('' != $Host)
                $this->mHost = $Host;
            if ('' != $User)
                $this->mUser = $User;
            if ('' != $Password)
                $this->mPassword = $Password;
                
            $this->connect($Database, $Host, $User, $Password);
        }
    }
    
    function connect($Database = '', $Host = '', $User = '', $Password = '') 
    {
        if ('' == $Database)
            $Database = $this->mDatabase;
        if ('' == $Host)
            $Host = $this->mHost;
        if ('' == $User)
            $User = $this->mUser;
        if ('' == $Password)
            $Password = $this->mPassword;
            
        $this->mLinkId = @mysql_connect($Host, $User, $Password);
        if (!$this->mLinkId) 
        {
            $this->halt($Database);
            return false;
        }
        
        if (!@mysql_select_db($Database, $this->mLinkId)) 
        {
            $this->halt($Database);
            return false;
        }
    
        return $this->mLinkId;
    }

    function free() 
    {
        if (is_resource($this->mQueryId))
            @mysql_free_result($this->mQueryId);
        $this->mQueryId = 0;
    }

    function query($QueryString) 
    {
        if ($QueryString == "")
            return false;

        if (!$this->mLinkId)
        {
            if (!$this->connect()) 
                return false; 
        }
        
        if ($this->mQueryId) 
            $this->free();

        $this->mQueryId = @mysql_query($QueryString, $this->mLinkId);

        if (!$this->mQueryId) 
        {
            $this->Errno = mysql_errno();
            $this->Error = mysql_error();
            $this->halt($QueryString);
            return false;
        }
        elseif(strcasecmp(substr(ltrim($QueryString), 0, 6), 'select') == 0)
        {
		    $ret = array();
    	    while(($row = @mysql_fetch_row($this->mQueryId)))
      		    $ret[] = $row;
		    
      		return $ret;
        }
        return $this->mQueryId;
    }

    function getFieldType($offset)
    {
        return mysql_field_type($this->mQueryId, $offset);
    }
    
    function getInsertId()
    {
        return @mysql_insert_id($this->mLinkId);
    }

    function getAffectedRows()
    {
    	return @mysql_affected_rows($this->mLinkId);
    }
    
    function duplicateRow($Id, $IdField, $Table)
    {
	   $result = @mysql_query("SELECT * FROM $Table WHERE $IdField='$Id'", $this->mLinkId);
	   if (!$result)
	       return false;
	       
	   $fields = @mysql_fetch_assoc($result); 
	   @mysql_free_result($result);
	
	   $columns_str = "";
	   $values_str = "";

	   foreach($fields as $field => $value)
	   {
	       if (!isset($fields[$field]))
	           $value = 'NULL';
	       else 
		       $value = '"'.mysql_escape_string($value).'"';
		  if ($field != $IdField) 
		  {
			 $columns_str .= $field.', ';
			 $values_str .= $value.', ';
		  }
	   }
	   $columns_str = substr($columns_str, 0, -2);
	   $values_str = substr($values_str, 0, -2);
	
	   $result = @mysql_query('INSERT INTO '.$Table.' ('.$columns_str.') VALUES ('.$values_str.')', $this->mLinkId);
	   if (!$result)
	       return false;
	   
	   $new_id = @mysql_insert_id();
	   @mysql_free_result($result);
	   return $new_id;
    }
    
    function halt($msg) 
    {
        $this->Error = @mysql_error($this->mLinkId);
        $this->Errno = @mysql_errno($this->mLinkId);

        if (!$this->mShowErrors)
            return;

        $this->haltmsg($msg);
    }

    function haltmsg($msg) 
    {
        printf("<b>Database error:</b> %s<br>\n", $msg);
        printf("<b>MySQL Error</b>: %s (%s)<br>\n", $this->mErrno, $this->mError);
    }
}
?>
