Location: PHPKode > scripts > Query Process > query-process/dblib.php
<?

    /*******************************************************************************
     **  File          :  dblib.php                                               **
     **  Class name    :  Query_process                                           **
     **  Author        :  P.Balakumar                                             **
     **                                                                           **
     **  Function      :  Getting a Query,Executing it and returning Arrays       **
     **                   based on user options.                                  **
     **                                                                           **
     **  Description   :  This class is database access wrapper that can access   **
     **                   MySQL or PosgresSQL or MsSql databases.                 **
     **                   It can establish connections to MySQL  or PosgreSQL or  **
     **                   MsSql server depending on the value set in a variable   **
     **                   of the class.                                           **
     **                   Then it can be used to execute queries and retrieve     **
     **                   result set data in arrays from any of the types  of     **
     **                   databases using the same class functions.               **
     **                                                                           **                                                    
     **  Date          :  23/06/2004                                              **
     **  Last Modified :  17/03/2005                                              **
     **                                                                           **
     **  PHP Version   :  5.0.3                                                   **
     *******************************************************************************/


    class Query_process
    {


         var $host;
         var $user_name   ="";
         var $pass_word   ="";
         var $database;      
         var $port_no     ="";
         var $sql_type;
         var $link;
         var $getquery;
         var $sqlflag;




               /***************************************************************
                 $gettype contains the type of sql ie ..MySQL , PgSQL or MsSql
                ***************************************************************/

                function __construct($get_type,$hname,$db,$u,$pw,$port)     //Constructor
                {
                     $this->sql_type    =  $get_type;    //$gettype -->MySQL or PgSQL
                     $this->host        =  $hname;    
                     $this->database    =  $db;        
                     $this->user_name   =  $u;    
                     $this->pass_word   =  $pw;    
                     $this->port_no     =  $port;    
                }


                function connect_to_mysql(){
                    $this->link    =  @mysql_connect($this->host,$this->user_name,$this->pass_word)or die(mysql_error());
                    @mysql_select_db($this->database,$this->link)or die(mysql_error());
                }

                function connect_to_pgsql(){
                    $this->pg_link =  pg_connect("host=$this->host port=$this->port_no dbname=$this->database user=$this->user_name password=root");
                    if(!$this->pg_link)  pg_errormessage();
                }

                function connect_to_mssql(){
                    $this->link    =  @mssql_connect($this->host,$this->user_name,$this->pass_word)or die(mysql_error());
                    @mssql_select_db($this->database,$this->link)or die("Incorrect database name");
                }


               /*========================================================
                 $query -->The Query to be processed.
                 $flag  -->Numbers 0,1,2,3,4 and 5.
                           0=>Single value
                           1=>Single Dimensional indexed array
                           2=>Two Dimensional indexed Array
                           3=>Single Dimensional Associative array
                           4=>Two Dimensional Associative array
                           5=>Execution of Insert,Update,Delete Queries
                 ========================================================*/


                function query_pass($query,$flag,$res="0")
                {

                     $this->getquery  =  $query;
                     $this->sqlflag   =  $flag;

                     if(strtolower($this->sql_type)=='mysql'){
                           $this->connect_to_mysql();
                                return $this->mysql_commands($query,$flag);
                     }
                     else if(strtolower($this->sql_type)=='pgsql'){
                           $this->connect_to_pgsql();
                                return $this->pgsql_commands($query,$flag);
                     }
                     else if(strtolower($this->sql_type)=='mssql'){
                           $this->connect_to_mssql();
                                return $this->mssql_commands($query,$flag);
                     }
                     else
                           echo 'Please pass the correct DB Server name ( MySQl or PgSQL or MsSql )';

                }



                function mysql_commands($q,$f)
                {


                     $result = @mysql_query($q,$this->link); // Executing the Query($q)

                     if(!$result)                // Checking whether the Query is Valid or not
                         return mysql_error();   // Returns error,if the Query is not a valid Query
                     else                        // Processing the Query,if it is a valid Query
                     {
                         switch($f)
                         {


                            case 0 :  return @mysql_result($result,0);
                                      break;

                            case 1 :  $row = @mysql_fetch_row($result);
                                      return $row;
                                      break;

                            case 2 :  while($row = @mysql_fetch_row($result))
                                         $ret_array[] = $row;
                                      return $ret_array;
                                      break;

                            case 3 :  $row = @mysql_fetch_array(($result),MYSQL_ASSOC);
                                      return $row;
                                      break;

                            case 4 :  while($row = mysql_fetch_array(($result),MYSQL_ASSOC))
                                         $ret_array[] = $row;
                                      return $ret_array;
                                      break;

                            case 5 :  if($result) 
                                          return true;
                                      else
                                          return false;
                                      break;

                            default:  return "Invalid Option";
                                      break;

                         }

                      }

               }


               function pgsql_commands($q,$f)
               {
                     $result=pg_query($this->pg_link,$q);

                     if(!$result)   echo pg_errormessage();

                     else
                     {

                         switch($f)
                         {


                            case 0 :  return @pg_result($result,0);
                                      break;

                            case 1 :  while ($row = @pg_fetch_row($result))
                                         $ret_array  = $row;
                                      return $ret_array;
                                      break;

                            case 2 :  while ($row = @pg_fetch_row($result))
                                         $ret_array[] = $row;
                                      return $ret_array;
                                      break;

                            case 3 :  while($row = @pg_fetch_assoc($result))
                                         $ret_array   = $row;
                                      return $ret_array;
                                      break;

                            case 4 :  while($row = @pg_fetch_assoc($result))
                                          $ret_array[]  = $row;
                                      return $ret_array;
                                      break;

                            case 5 :  if($result) 
                                          return true;
                                      else
                                          return false;
                                      break;

                            default:  return "Invalid Option";
                                      break;


                         }

                      }


               }


                function mssql_commands($q,$f)
                {

                     $result = @mssql_query($q,$this->link); // Executing the Query($q)

                     if(!$result)                // Checking whether the Query is Valid or not
                         return "Error in query";   // Returns error,if the Query is not a valid Query
                     else                        // Processing the Query,if it is a valid Query
                     {
                         switch($f)
                         {


                            case 0 :  return @mssql_result($result,0);
                                      break;

                            case 1 :  $row = @mssql_fetch_row($result);
                                      return $row;
                                      break;

                            case 2 :  while($row = @mssql_fetch_row($result))
                                          $ret_array[] = $row;
                                      return $ret_array;
                                      break;

                            case 3 :  $row = @mssql_fetch_array($result);
                                      return $row;
                                      break;

                            case 4 :  while($row = mssql_fetch_array($result))
                                          $ret_array[] = $row;
                                      return $ret_array;
                                      break;

                            case 5 :  if($result) 
                                          return true;
                                      else
                                          return false;
                                      break;

                            default:  return "Invalid Option";
                                      break;

                         }

                      }

               }



    }



?>
Return current item: Query Process