[DUG] Delphi PHP

Neven MacEwan neven at mwk.co.nz
Tue May 20 09:36:03 NZST 2008


Rob

I'm using postgesql in my current project, very nice even under windows, 
PHP is quite easy to get to get to grip with

This is my TDataset class as an example, note the hacky support of ODBC 
& PGSQL

  class TDataSet {

    public  $Data = null;
    private $Statement = null; // can be pdo or odbc resuore
    private $RowNumber = 0;
    private $DriverType;
    private $SQL;
    public  $ColumnNames = array();

    public function __construct($Statement, $DriverType, $SQL = null) {
      // Copy any required data from the  $Statement
      set_error_handler(TDatabaseErrorHandler, E_WARNING);
      if (isset($SQL)) {$this->SQL = $SQL;}
      $this->DriverType = $DriverType;
      $this->Statement = $Statement;
      $this->RowCount();
      restore_error_handler();
    }              
   
    public function RowCount() { // actually fetches all the data
      try {
        switch ($this->DriverType) {
          case TDatabase::tdtODBC; 
            if (!isset($this->Data)) {
              $this->Data = array();
              odbc_longreadlen($Result, 4194304);
              odbc_binmode($Result, ODBC_BINMODE_CONVERT);
              $ColCount = odbc_num_fields($this->Statement);
              for ($I = 1; $I < $ColCount; $I++) {
                $this->ColumnNames[] = odbc_field_name($this->Statement, 
$I);
              }  
              while ($Row = odbc_fetch_array($this->Statement)) {
                $this->Data[] = $Row; 
              }
            }
            break;
          case TDatabase::tdtPGSQL: 
            if (!isset($this->Data)) {
              $this->Data = array();
//              odbc_longreadlen($Result, 4194304);
//              odbc_binmode($Result, ODBC_BINMODE_CONVERT);
              $ColCount = pg_num_fields($this->Statement);
              for ($I = 0; $I < $ColCount; $I++) {
                $this->ColumnNames[] = pg_field_name($this->Statement, $I);
              }  
              while ($Row = pg_fetch_array($this->Statement, NULL, 
PGSQL_ASSOC)) {
                $this->Data[] = $Row; 
              }
            }
            break;
        }
     } catch (Exception $e) {
       self::SendLastError($this->SQL, 'Error Fetching Data');
       return false;
     } 
     return count($this->Data);
    }

    public function FetchRow($RowNumber = null) {
      if (isset($RowNumber)) {$this->RowNumber = $RowNumber;}
      if ($this->RowCount() > $this->RowNumber) {
        $Result = array();
        foreach($this->Data[$this->RowNumber] as $Key => $Value) {
          if (is_string($Value)) {
            $Result[$Key] = trim($Value);
          } else {
            $Result[$Key] = $Value;
          }
        }
        $this->RowNumber++;
      } else {$Result = false;}
      return $Result;
    }
   
    public function SelectorData($NullBlank = false) {
      // takes the first 2 columns and return a Col1 => Col2 2 array
      // if a single coluum use it for both values
      $Result = array();
      if ($NullBlank) {
        $Result[null] = '';
      } 
      while ($Row = $this->FetchRow()) {
        if (!isset($Keys)) {
          $Keys = array_keys($Row);
          if (sizeof($Keys) < 2) {$Keys[1] = $Keys[0];}
        } 
        $Result[$Row[$Keys[0]]] = $Row[$Keys[1]];
      }
      return $Result;
    } 

  } // end class  TDataset
> Not yet
>
> I am just looking into a web site project at the moment.  I haven't 
> done any PHP dev before (just learning it) so I thought that using a 
> familiar interface and database would speed development.  If I do 
> proceed I am planning on doing a lot of the processing in Stored 
> procedures rather than via PHP so my knowledge of FB comes in handy 
> (as opposed to MySQL).
>
>



More information about the Delphi mailing list