Location: PHPKode > projects > News Manager - RSS aggregator > newsmanager/db/db.php
<?php
	  /*******************************************************************************
       **  File          :  db.php 		                            	          	**
       **  Author        :  G.Ramkumar												**
       **  Description   :  Connecting to the Pgsql,mysql,mssql Database   			**
       **					  			  										    **
       **  				  										                  	**
       **  Date          :  14/07/2005                                              **
       **  Last Modified :  02/08/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 $pg_link;
         var $getquery;
         var $sqlflag;


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

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


                }
		/*
                function connect_to_pgsql()
                {
                   $this->pg_link =  pg_connect("host=$this->host port=$this->port_no dbname=$this->database user=$this->user_name password=$this->password");
                   if(!$this->pg_link)
                      pg_errormessage();
                }
        */
        		function connect_to_pgsql()
				{
					   if (!empty($this->host)) {
								$this->connstring .= " host=$this->host";
						}
						if (!empty($this->port_no)) {
								$this->connstring .= " port=$this->port_no";
						}
						if (!empty($this->database)) {
								$this->connstring .= " dbname=$this->database";
						}
						if (!empty($this->user_name)) {
								$this->connstring .= " user=$this->user_name";
						}
						if (!empty($this->pass_word)) {
								$this->connstring .= " password=$this->pass_word";
						}

						$this->pg_link = pg_connect ($this->connstring);

						if(!$this->pg_link)
							  pg_errormessage();
				}

				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_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(mssql_error());
				}


               /*========================================================
                 $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';

                }

				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_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 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 mssql_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 @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;

                         }

                      }

               }


    } //CLASS ENDS
?>
Return current item: News Manager - RSS aggregator