<?php

/** 
 * @class mysql
 *
 * @author Benjamin Mercier
 * @date 01/03/2009
 * @modified Xavier Perrissoud
 * @date 28/05/2009
 * @version 0.1
 *
 * @message getInsertId and getNumRows are modified. For use : fetchObject, ou execQuery and after getInsertId/getNumrows
 * @author Mogui
 * @date 19/05/2009 - 15h30
*/
class mysql {
	
	public $executed_req = 0;
	private $link = NULL;

	/**
	 * @brief Connect to mySql
	 * @param mysql_user -> Name of the mysql user
	 * @param mysql_passwors -> Password of the mysql user
	 * @param mysql_server -> MySQL server
	 * @param database -> Database of the website
	 * @return TRUE -> Connection to mysql has generated no error.
	*/
	public function connect($mysql_user, $mysql_password, $mysql_server, $database)
	{
		if ( !$this->link = mysqli_connect($mysql_server, $mysql_user, $mysql_password, $database) ) {
			throw new myException('Error when connecting to MySQL :'.mysqli_connect_error());
		} else {
			return TRUE;
		} 
	} // End of connect

	/**
	 * @brief Execute an request
	 * @param Request in SQL format.
	 * @return TRUE -> Query has generated no error.
	*/
	public function execRequest($request)
	{
		if ( @mysqli_query($this->link, $request) ) {
			$this->executed_req++;
			return TRUE;
		} else {
			throw new myException("Error when query is executed : \n$request\n".mysqli_error($this->link));
		}
	} // End of execRequest


	/** 
	 * @brief Execute an request and fetch array
	 * @param Request in SQL format
	 * @return Array of False -> FALSE: no record found
	*/
	public function fetchArray($request)
	{
		if ( !@$handle = mysqli_query($this->link, $request) ) {
			throw new myException("Error when query is executed :\n$request\n".mysqli_error($this->link));
			}
			$this->executed_req++;
			$result = array();
			while( $row = mysqli_fetch_assoc($handle) )
			{
				$result[] = $row;
			}

			if ( is_null($result) ) {
				return FALSE;
			} else {
				return $result;
			}
	} // End of fetchArray

	/**
	 * @brief Execute an request and return an object
	 * @param Request in SQL format
	 * @return Array of False -> FALSE: no record found
	*/
	public function fetchObject($request)
	{
			if ( !@$handle = mysqli_query($this->link, $request) ) {
			throw new myException("Error when query is executed :\n$request\n".mysqli_error($this->link));
			}
			$this->executed_req++;
			$result = array();
			while( @$row = mysqli_fetch_object($handle) )
			{
				$result[] = $row;
			}
			if ( is_null($result) ) {
				return FALSE;
			} else {
				return $result;
			}
	} // End of fetchObject

	/**
	 * @brief Return the id of the last inserted object
	 * @return Id of the last inserted object FROM LAST REQUEST
	 * @modified Mogui
	 * @date 19/05/2009 - 15:41
	*/
	public function getInsertId()
   	{
		$result = mysqli_insert_id($this->link);
		return $result;
	} // End of getInsertId
    
    /**
	 * @brief return the number of affected entries
	 * @return number of affected entries FROM LAST REQUEST
	 * @modified Mogui
	 * @date 19/05/2009 - 15:38
	*/
	public function getNumRows()
   	{
        	$result = mysqli_num_rows($this->link);
		return $result;

	} // End of getId
	
	/** 
	 * @brief Clear string before send in sql req.
	 * @param value -> Value to clear
	 * @return Value cleared
	*/
	public function clearString($string)
	{
		return mysqli_real_escape_string($this->link, $string);
	} // End of clearString


} // End of mysql


?>
