<?php
// $Id: class.mysql_database.php,v 1.4 2005/10/28 11:56:02 ohwada Exp $

// 2005-07-25 K.OHWADA
// BUG 2793: Fatal error: Call to undefined function: _print_sql_error()

// 2004-11-28 K.OHWADA
// substitute for class XoopsMySQLDatabase

//=========================================================
// class mysql_database
// substitute for class XoopsMySQLDatabase
// 2004-10-20 K.OHWADA
//=========================================================

class mysql_database extends Database
{

// Database connection
	var $conn;

// debug
// BUG 2793: Fatal error: Call to undefined function: _print_sql_error()
	var $flag_print_error = 1;


//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function mysql_database()
{
// dummy
}

//---------------------------------------------------------
// function
//---------------------------------------------------------
function connect()
{
	$this->conn = @mysql_connect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS);

	if (!$this->conn) 
	{
		$this->_print_error();
		return false;
	}

	if (!mysql_select_db(XOOPS_DB_NAME)) 
	{
		$this->_print_error();
		return false;
	}

	return true;
}

function fetchRow($result)
{
	return @mysql_fetch_row($result);
}

function fetchArray($result)
{
	return @mysql_fetch_assoc( $result );
}

function fetchBoth($result)
{
	return @mysql_fetch_array( $result, MYSQL_BOTH );
}

function getInsertId()
{
	return mysql_insert_id($this->conn);
}

function getRowsNum($result)
{
	return @mysql_num_rows($result);
}

function getAffectedRows()
{
	return mysql_affected_rows($this->conn);
}

function close()
{
	mysql_close($this->conn);
}

function freeRecordSet($result)
{
	return mysql_free_result($result);
}

function error()
{
	return @mysql_error();
}

function errno()
{
	return @mysql_errno();
}

function quoteString($str)
{
	$str = "'".str_replace('\\"', '"', addslashes($str))."'";
	return $str;
}

function &queryF($sql, $limit=0, $start=0)
{
	if ( !empty($limit) ) 
	{
		if (empty($start)) 
		{
			$start = 0;
		}
	
		$sql = $sql. ' LIMIT '.(int)$start.', '.(int)$limit;
	}

	$result =& mysql_query($sql, $this->conn);

	if ( !$result )
	{

// BUG 2793: Fatal error: Call to undefined function: _print_sql_error()
// wrong function name
		$this->_print_error($sql);

		return false;
    }

	return $result;
}

function &query($sql, $limit=0, $start=0)
{
	return $this->queryF($sql, $limit, $start);
}

//---------------------------------------------------------
// debug
//---------------------------------------------------------
function _print_error($sql='')
{
	if ( !$this->flag_print_error )  return;

	if ($sql)
	{
		echo "sql: $sql <br />\n";
	}

	echo "<font color='red'>".$this->error()."</font><br />\n";	
}

//---------------------------------------------------------
}

?>