<?php
require_once('config3.php');


class MySQLDatabaseAdmission {
	private $connection3;
	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()
    {
       // echo "<br> Connect: Data".DB_SERVERS.' '.DB_USERS.' '.DB_PASSS.' '.DB_NAMES;
        $this->connection3 = mysqli_connect(DB_SERVERS, DB_USERS, DB_PASSS);
        if (!$this->connection3) {
           die("Database connection failed: " . mysql_error());
           $this->error("Connection could not be established.");

        } else {
            $db_select = mysqli_select_db( $this->connection3, DB_NAMES );
            // if (!$db_select) {

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

        }
    }

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

    }

    public function query($sql)
    {
        $result = mysqli_query($this->connection3, $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->connection3,$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->connection3);
        // }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->connection3);   
        // }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->connection3) . "<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>');
    }
}
        
        
        
  public static function find_all()
    {
        global $database3;
        return self::find_by_sql("SELECT * FROM " . self:: $table_name);
        //return $result_set;

    }

    public static function find_by_id($id = 0)
    {
        global $database3;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE id={$id} LIMIT 1");
       
        return !empty($result_array) ? array_shift($result_array) : false;

    }

    public static function find_by_sql($sql = "")
    {
        global $database3;
        $result_set = $database3->query($sql);
        $object_array = array();
        while ($row = $database3->fetch_array($result_set)) {
            $object_array[] = self::instantiate($row);
        }
        return $object_array;

    }

    private static function instantiate($record)
    {

        $object = new self;
        foreach ($record as $attribute => $value) {
            if ($object->has_attribute($attribute)) {
                $object->$attribute = $value;
            }

        }
        return $object;
    }

    private function has_attribute($attribute)
    {

        $object_vars = get_object_vars($this);
        return array_key_exists($attribute, $object_vars);
    }

    public function save()
    {
        return isset($this->id) ? $this->update() : $this->create();
    }

    public function create()
    {
        global $database;
        $sql = "INSERT INTO " . self::$table_name . " (";
        $sql .= "formno, email";
        $sql .= ") 	VALUES ('";
        $sql .= $database3->escape_value($this->formno) . "', '";
        $sql .= $database3->escape_value($this->email) . "') '";
        if ($database3->query($sql)) {
            $this->id = $database3->insert_id();
            return true;
        } else {
            return false;
        }

    }





  



}

$database3 = new MySQLDatabaseAdmission();

?>