<?php
require_once(LIB_PATH.DS.'config2.php');

class MySQLDatabase2 {
	private $connection2;
	public $last_query;
	private $magic_quotes_active;
	private $real_escape_string_exists;
	
	
	
	    function __construct()
    {
        $this->open_connection();
        $this->magic_quotes_active = get_magic_quotes_gpc();
        $this->real_escape_string_exists = function_exists("mysqli_real_escape_string");

    }

    public function open_connection()
    {
        $this->connection2 = mysqli_connect(DB_SERVER2, DB_USER2, DB_PASS2);
        if (!$this->connection2) {
           die("Database connection failed: " . mysqli_error());
           $this->error("Connection could not be established.");

        } else {
            $db_select = mysqli_select_db( $this->connection2, DB_NAME2 );
            if (!$db_select) {

               die("Database selection failed " . mysql_error());
               // $this->error("Database selection failed.");
            }else{
                 //$this->error("Database3455 selection successful.");
            }

        }
    }

    public function close_connection()
    {
        if (isset($this->connection2)) {
            mysql_close($this->connection2);
            unset($this->connection2);
        }

    }

    public function query($sql)
    {
        $result = mysqli_query($this->connection2, $sql );
       $this->last_query = $sql;
        $this->confirm_query($result);
        return $result;

    }

    public function escape_value($value)
    {

        if ($this->real_escape_string_exists) {

            if ($this->magic_quotes_active) {
                $value = stripslashes($value);
            }
            $value = mysqli_real_escape_string($this->connection2,$value);
        } else {
            if (!$this->magic_quotes_active) {
                $value = addslashes($value);
            }
        }
        return $value;
    }

// Database neutral methods
    public function fetch_array($result_set)
    {

        // if (mysql_fetch_array($result_set)){
            return mysqli_fetch_array($result_set);
           
        //  }else {
        //      $this->error("Could not fetch selected rows from the database.");
        // }
    }

    public function num_rows($result_set)
    {
         //if ($row= mysql_num_rows($result_set)){
            return  mysqli_num_rows($result_set);
        // }else {
        //     $this->error("Row count from database query failed.");
        // }
    }

    public function insert_id()
    {
        // if ($row= mysql_insert_id($this->connection)){
            return mysqli_insert_id($this->connection2);
        // }else {
        //     $this-error("Could not insert the last data-id");
        // }
       
    }

    public function affected_rows()
    {
        // if (mysql_affected_rows($this->connection)){

            return  mysqli_affected_rows($this->connection2);   
        // }else {
        //      $this->error("No rows was affected");
        //    }
}
    private function confirm_query($result)
    {

        if (!$result) {
          //  $this-error("Operation was not successful. ". mysql_error($this->connection));
             $output = "Database query failed. " . mysqli_error($this->connection2) . "<br/><br/>";
             $output = "Last SQL Query: " . $this->last_query;
           // $this->error("Database query failed");
            $this->error($output);
        }
    }

   private function error ($error=""){
    if ($error!=""){
        die('<style="background-color:brown; padding:10px; border-radius:10px; width:100%">'.$error.'</style>');
    }
}

}

$database2 = new MySQLDatabase2();








	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*function __construct(){
	$this->open_connection();	
	$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists	= function_exists("mysql_real_escape_string");

	}
	
	public function open_connection(){
	$this->connection= mysql_connect(DB_SERVER, DB_USER,DB_PASS);
// 	$this->db->query('SET SQL_BIG_SELECTS=1');
	if (!$this->connection){
	die("Database connection failed: ".mysql_error());	
		
	} else {
		$db_select = mysql_select_db(DB_NAME, $this->connection);
		if (!$db_select){
			
		die("Database selection failed ". mysql_error());	
		}
		
	}
	}
	public function close_connection(){
	if (isset($this->connection)){
		mysql_close($this->connection);
		unset($this->connection);
	}
		
	}
	
	public function query($sql){
		$result = mysql_query($sql, $this->connection);
		$this->last_query=$sql;
// 		$this->db->query('SET SQL_BIG_SELECTS=1');
		$this->confirm_query($result);
		return $result;
		
	}
	
	public function escape_value($value){
	
if ($this->real_escape_string_exists){
	
	if ($this->magic_quotes_active){
		$value = stripslashes($value);
	}
		$value = mysql_real_escape_string($value);
	}else {
	if (!$this->magic_quotes_active){
		$value = addslashes($value);
	}
	}
	return $value;
}

// Database neutral methods
public function fetch_array($result_set){
return mysql_fetch_array($result_set);	
}
public function num_rows($result_set){
return mysql_num_rows($result_set);	
}
public function insert_id(){
return mysql_insert_id($this->connection);	
}
public function affected_rows(){
return mysql_affected_rows($this->connection);	
}	
	private function confirm_query($result){
		
	if (!$result){
//  $output ="Database query failed. ".mysql_error()."<br/><br/>";	
//  $output.="Last SQL Query: ". $this->last_query;
//  die($output);
  header("Location:http://eportal.oscotechesaoke.edu.ng/admin_portal1/dashboard.php");
	}
	}
	
}
$database = new MySQLDatabase();
*/
?>