PHPPDO use instead of PDO drivers

Discuss everything related to ApPHP AdminPanel. Any questions related to developing or using should be posted to this forum.

Moderator: alexandrleonenko

Wampumash
Junior
Junior
Posts: 13
Joined: Nov 14th, '12, 06:21

PHPPDO use instead of PDO drivers

Postby Wampumash » Nov 14th, '12, 06:32

How can I use PHPPDO instead of standard PDO drivers. PHPPDO is an alternative when you can't get drivers installed on the server you are trying to get this to work on. heres a link.
http://phppdo.sourceforge.net/. Please help. Server Op won't install PDO extensions and this is the only alternative i found...

administrator
Site Admin
Site Admin
Posts: 6079
Joined: Jan 7th, '09, 23:18
Contact:

Re: PHPPDO use instead of PDO drivers

Postby administrator » Nov 14th, '12, 08:49

We don't know how to use it- you have to read instructions and replace all database functionality with it.

BTW, PDO is a part of a Standard PHP package (!!!) from v.5.0.0, so if your server don't want to install it - simply find more user-friendly hosting.
For example: site5 [dot] com

Wampumash
Junior
Junior
Posts: 13
Joined: Nov 14th, '12, 06:21

Re: PHPPDO use instead of PDO drivers

Postby Wampumash » Nov 15th, '12, 07:04

Can I post code changes here to make this work? As far as I know I made it work or would you have them sent to you via private chat or whatever?...This would be an awesome workaround for so many people. And it seemed to work for me. With a few changes and folder placement of specific files. I used the source above and did a file replace search and got it to work with 1 include. Maybe you should look at this again...

administrator
Site Admin
Site Admin
Posts: 6079
Joined: Jan 7th, '09, 23:18
Contact:

Re: PHPPDO use instead of PDO drivers

Postby administrator » Nov 15th, '12, 07:13

Yes, you may post your changes here!

Wampumash
Junior
Junior
Posts: 13
Joined: Nov 14th, '12, 06:21

Re: PHPPDO use instead of PDO drivers

Postby Wampumash » Nov 18th, '12, 05:26

I placed the phppdo folder in /inc/classes/phppdo and then modified the datatbase.class.php file to as follows below. This allows for a PHP database "driver".
Take in mind i hacked it in quickly and the primary changes to make it work are the PHPPDO lines and the include at the top of the file.. I tried this on 2 servers
with linux backends and mysql only. I have no idea if this works with other sql servers.
Hope this helps anyone with problems with PDO support.
And you are welcome.. And thank the people who made PHPPDO....

Code: Select all

<?php

/**
 *   Class Database (PDO Extension)
 *  ----------------------------
 *  Description : encapsulates database operations & properties with PHPPDO
 *  Updated       : 17.08.2011
 *  Version     : 1.0.3
 *   Written by  : ApPHP
 *   Syntax (standard)  : $db = new Database($database_host, $database_name, $database_username, $database_password, EI_DATABASE_TYPE, $is_installation);
 *   Syntax (singleton) : $db = Database::GetInstance($database_host, $database_name, $database_username, $database_password, EI_DATABASE_TYPE, $is_installation);
 *
 *  PUBLIC           STATIC             PROTECTED           PRIVATE
 *  -------          ----------          ----------          ----------
 *  __construct      GetInstance
 *   __destruct       IsConnected
 *   Create           
 *   Open             
 *   Close
 *   GetVersion
 *   GetDbDriver
 *   Query
 *   Exec
 *   AffectedRows
 *   RowCount
 *   ColumnCount
 *   InsertID
 *   SetEncoding
 *   Error
 *   ErrorCode
 *   ErrorInfo
 *   FetchAssoc
 *   FetchArray
 *
 *   CHANGE LOG
 *   -----------   
 *  1.0.3
 *      - improved Exec() - added $check_error parameter
 *      - improved Query() - added fetch type
 *      -
 *      -
 *      -
 *  1.0.2
 *     - added FetchAssoc()
 *     - fixed bug in RowCount
 *     - fixed bug in GetInstance()
 *     - added IsConnected()
 *     - fixed error with $installation property
 *  1.0.1
 *     - added GetDbDriver
 *     - improved GetVersion()
 *     - added Create()
 *     - added GetInstance()
 *     - added default params for SetEncoding()
 *   
 **/
//Wampum -- added require
require_once("phppdo/phppdo.php");

class Database
{
    // connection parameters
   

   
   private $host = "";
   private $port = "";
   private $db_driver = "";
    private $database = "";
    private $user = "";
    private $password = "";
   private $force_encoding = false;
   private static $installation = false;

   private $error = "";
   
   private $affectedRows = "0";

   // database connection handler
    private $dbh = NULL;
   
   // database statament handler
   private $sth = NULL;
   
   // static data members   
   private static $objInstance;


   //==========================================================================
    // Class Constructor
   //       @param $database_host
   //       @param $database_name
   //       @param $database_username
   //       @param $database_password
   //       @param $db_driver
   //       @param $force_encoding
   //       @param $is_installation
   //==========================================================================
    function __construct($database_host="", $database_name="", $database_username="", $database_password="", $db_driver="", $force_encoding=false, $is_installation=false)   
    {
      $this->host     = $database_host;
      $this->port     = "";
      
      $host_parts     = explode(":", $database_host);      
      if(isset($host_parts[1]) && is_numeric($host_parts[1])){
         $this->host = $host_parts[0];   
         $this->port = $host_parts[1];   
      }
      
      if($database_host == ""){
         $config = new Config();   
         $this->host = $config->getHost();
         $this->user = $config->getUser();
         $this->password = $config->getPassword();
         $this->database = $config->getDatabase();
         $this->db_driver = $config->getDatabaseType();
         $this->force_encoding = $force_encoding;
      }else{
         $this->database  = $database_name;      
         $this->user     = $database_username;
         $this->password  = $database_password;
         $this->db_driver = strtolower($db_driver);
         $this->force_encoding = $force_encoding;
         
      }
      
      self::$installation = ($is_installation) ? true : false;
   }

   //==========================================================================
    // Class Destructor
   //==========================================================================
    function __destruct()
   {
      // echo 'this object has been destroyed';
    }

    /**
     *   Create database
     */
    public function Create()
    {
      $this->dbh = new PHPPDO($this->db_driver.":host=".$this->host, $this->user, $this->password);
      $this->dbh->exec("CREATE DATABASE IF NOT EXISTS `".$this->database."`;");
      if($this->dbh->errorCode() != "00000"){
         $err = $this->dbh->errorInfo();
         $this->error = $err[2];
         return false;
      }
      return true;
   }

    /**
     *   Checks and opens connection with database
     */
    public function Open()
    {
                                 //WAMPUM -- commented version check to get it to work
      //if(version_compare(PHP_VERSION, '5.0.0', '<') || !defined('PDO::ATTR_DRIVER_NAME')){
         //$this->error = "You must have PHP 5 or newer installed to use PHP Data Objects (PDO) extension";
         //return false;
      //}

      $port = (!empty($this->port)) ? ";port=".$this->port : "";

      try{
                                                //WAMPUM  --- changed PDO to PHPPDO -- check for driver availibility- only needed mysql for my purposes
         switch($this->db_driver){
            case "mssql":
               $this->dbh = new PHPPDO("mssql:host=".$this->host.$port.";dbname=".$this->database, $this->user, $this->password);
               break;
            case "sybase":
               $this->dbh = new PHPPDO("sybase:host=".$this->host.$port.";dbname=".$this->database, $this->user, $this->password);
               break;
            case "sqlite":
               $this->dbh = new PHPPDO("sqlite:my/database/path/database.db");
               break;
            case "pgsql":
               $this->dbh = new PHPPDO("pgsql:host=".$this->host.$port.";dbname=".$this->database, $this->user, $this->password);
               break;
            case "mysql":
            default:
               $this->dbh = new PHPPDO($this->db_driver.":host=".$this->host.$port.";dbname=".$this->database, $this->user, $this->password);
               break;
         }
         $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         if(empty($this->dbh)){
            return false;
         }else if($this->force_encoding){
            $this->dbh->exec("set names utf8");
         }
      }catch(PDOException $e){ 
         $this->error = $e->getMessage();
         return false;
      }           

        return true;
    }   
   
    /**
     *   Close connection
     */
    public function Close()
    {
      $this->sth = null;
      $this->dbh = null;
    }

    /**
     *   Returns database engine version
     */
   public function GetVersion()
   {
      // clean version number from alphabetic characters
      $version = $this->dbh->getAttribute(PDO::ATTR_SERVER_VERSION);
      return preg_replace("/[^0-9,.]/", "", $version);
   }

    /**
     *   Get DB driver
     */
    public function GetDbDriver()
    {
      return $this->db_driver;
    }

    /**
     *   Runs query
     *      @param $query
     */
    public function Query($query = '', $fetch_mode = PDO::FETCH_ASSOC)
    {
      try{ 
         $this->sth = $this->dbh->query($query);
         if($this->sth !== FALSE){
            //if(!empty($fetch_mode))
            $this->sth->setFetchMode($fetch_mode);
            return $this->sth;
         }
         else return false;
      }catch(PDOException $e){
         $this->error = $e->getMessage();
         
         if(!self::$installation){
            $error_no = $e->getCode();
            $error_descr  = "ENV:        ". $_SERVER['SERVER_NAME']."<br><br>";
            $error_descr .= "TIME:       ".@date("M d, Y g:i A")."<br><br>";
            $error_descr .= "SCRIPT:     ".$_SERVER['PHP_SELF']."<br><br>";
            $error_descr .= "ERROR LINE: ".(int)$e->getLine()."<br><br>";
            $error_descr .= "ERROR:      ".$this->error."<br><br>";
            $error_descr .= "QUERY:      ".$query."<br><br>";
            $current_file = basename($_SERVER['PHP_SELF'], ".php").".php";
            
            $ip_address = get_ip_address();
            $sql_log = "INSERT INTO ".TABLE_SYSTEM_LOGS." (id, log_type, title, file_name, log, ip_address, date_created)
                      VALUES (NULL, 'Error', 'DB Error #".$error_no."', '".$current_file."', '".encode_text($error_descr)."', '".$ip_address."', '".@date("Y-m-d H:i:s")."')";
            $this->Exec($sql_log, false);
         }
         return false;
      }           
    }

    /**
     *   Executes query
     *      @param $query
     */
    public function Exec($query = '', $check_error = true)
   {
      try{
         $this->affectedRows = $this->dbh->exec($query);
         return $this->affectedRows;   
      }catch(PDOException $e){         
         if($check_error){
            $this->error = $e->getMessage();            
            if(!self::$installation){
               $error_no = $e->getCode();
               $error_descr  = "ENV:        ". $_SERVER['SERVER_NAME']."<br><br>";
               $error_descr .= "TIME:       ".@date("M d, Y g:i A")."<br><br>";
               $error_descr .= "SCRIPT:     ".$_SERVER['PHP_SELF']."<br><br>";
               $error_descr .= "ERROR LINE: ".(int)$e->getLine()."<br><br>";
               $error_descr .= "ERROR:      ".$this->error."<br><br>";
               $error_descr .= "QUERY:      ".$query."<br><br>";
               $current_file = basename($_SERVER['PHP_SELF'], ".php").".php";
               
               $ip_address = get_ip_address();
               $sql_log = "INSERT INTO ".TABLE_SYSTEM_LOGS." (id, log_type, title, file_name, log, ip_address, date_created)
                         VALUES (NULL, 'Error', 'DB Error #".$error_no."', '".$current_file."', '".encode_text($error_descr)."', '".$ip_address."', '".@date("Y-m-d H:i:s")."')";
               $this->Query($sql_log);
            }            
         }         
         return false;
      }      
   }

    /**
     *   Set encoding and collation on database
     *      @param $encoding
     *      @param $collation
     */
    public function SetEncoding($encoding = "utf8", $collation = "utf8_unicode_ci")
    {      
      if(empty($encoding)) $encoding = "utf8";
        if(empty($collation)) $collation = "utf8_unicode_ci";   
        $sql_variables = array(
                'character_set_client'  =>$encoding,
                'character_set_server'  =>$encoding,
                'character_set_results' =>$encoding,
                'character_set_database'=>$encoding,
                'character_set_connection'=>$encoding,
                'collation_server'      =>$collation,
                'collation_database'    =>$collation,
                'collation_connection'  =>$collation
        );
        foreach($sql_variables as $var => $value){
            $sql = "SET $var=$value;";
            $this->Query($sql);
        }       
    }

    /**
     *   Returns affected rows after exec()
     */
    public function AffectedRows()
    {
      return $this->affectedRows;
    }   

    /**
     *   Returns rows count for query()
     */
    public function RowCount()
    {
      return $this->sth->rowCount();
    }      

    /**
     *   Returns columns count for query()
     */
    public function ColumnCount()
    {
      return $this->sth->columnCount();
    }      

    /**
     *   Returns last insert ID
     */
   public function InsertID()
    {
      return $this->dbh->lastInsertId();
    }

    /**
     *   Returns error
     */
    public function Error()
    {
      return $this->error;      
    }
   
    /**
     *   Returns error code
     */
    public function ErrorCode()
    {
      return $this->dbh->errorCode();
    }

    /**
     *   Returns error code
     */
    public function ErrorInfo()
    {
      return $this->sth->errorInfo();
    }
 
   /**
    * Fetch assoc
    */
    public function FetchAssoc()
    {
      return $this->sth->fetch(PDO::FETCH_ASSOC);
    }
 
   /**
    * Fetch array
    */
    public function FetchArray()
    {
      return $this->sth->fetch(PDO::FETCH_BOTH);
    }

   //==========================================================================
    // Returns DB instance or create initial connection
   //       @param $database_host
   //       @param $database_name
   //       @param $database_username
   //       @param $database_password
   //       @param $db_driver
   //       @param $force_encoding
   //       @param $is_installation
   //==========================================================================
   public static function GetInstance($database_host = "", $database_name = "", $database_username = "", $database_password = "", $db_driver = "", $force_encoding = false, $is_installation = false)
   {
      $database_port = "";
      
      $host_parts = explode(":", $database_host);      
      if(isset($host_parts[1]) && is_numeric($host_parts[1])){
         $database_host = $host_parts[0];   
         $database_port = $host_parts[1];   
      }
      
      if($database_host == ""){
         $config = new Config();   
         $database_host = $config->getHost();
         $database_name = $config->getDatabase();
         $database_username = $config->getUser();
         $database_password = $config->getPassword();         
         $db_driver = $config->getDatabaseType();
      }
      
      if(!self::$objInstance){
         self::$objInstance = new Database($database_host, $database_name, $database_username, $database_password, $db_driver, $force_encoding, $is_installation);
         self::$objInstance->Open();
        }      
        return self::$objInstance;
   }
   
   /**
    * Check if connected
    */
   public static function IsConnected()
   {
      return (self::$objInstance) ? true : false;
   }
   
}

?>


This allowed the initial install on a server without PDO extensions, but i have ran into some bugs... Like changing usernames and passwords would lock
out users, for example.. 2nd install on the one server seemed to clear that problem up. Do not use MD5 encryption..

Wampumash
Junior
Junior
Posts: 13
Joined: Nov 14th, '12, 06:21

Re: PHPPDO use instead of PDO drivers

Postby Wampumash » Nov 18th, '12, 05:38

I also want to point out that 1 of the servers that actually supported all of the requirements, half the time after the initial install i couldn't login to the panel and it would return a 505 error. I have no idea what thats about. But after I addded PHPPDO to the servers all requests seemed to work perfectly and no server errors...You should support this to the very end and actually use it more.Best server backend i found yet..

administrator
Site Admin
Site Admin
Posts: 6079
Joined: Jan 7th, '09, 23:18
Contact:

Re: PHPPDO use instead of PDO drivers

Postby administrator » Nov 18th, '12, 08:03

thanks, hope it will be useful for other members as well.

Wampumash
Junior
Junior
Posts: 13
Joined: Nov 14th, '12, 06:21

Re: PHPPDO use instead of PDO drivers

Postby Wampumash » Jul 1st, '16, 05:30

I'm creeping around again. Hi....


Return to “ApPHP AdminPanel {developers/users}”