home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / sybaseclass.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  4.7 KB  |  214 lines

  1. SyBASE Classes 
  2.  
  3.  
  4. Database and Recordset classes fo SyBASE Usage is obvious. 
  5.  
  6.  
  7.  
  8.  
  9.  
  10. <?php 
  11.  
  12. //  
  13. //    Author:  Razvan  STANESCU  <pappy@gecad.ro>  
  14. // 
  15. //    You may use this code as long as you leave this note 
  16. // 
  17.  
  18. // 
  19. //    Usage example 
  20. // 
  21. //    require("sybase.php3"); 
  22. // 
  23. //    $sybdb = new CSybDb("SYBASE", "http_user", "http_pass", "http_db"); 
  24. //    $sybrs = new CSybRs("select * from http_log", $sybdb); 
  25. // 
  26. //    $sybrs->display();    //    ... or do anything else 
  27. // 
  28. //    $sybrs->destroy(); 
  29. //    $sybdb->destroy(); 
  30. // 
  31.  
  32. class CSybDb 
  33.     var        $sybdb; 
  34.  
  35.     function CSybDb($server, $user, $pass, $dbname =  "") 
  36.     { 
  37.         $this->sybdb = sybase_connect($server, $user, $pass); 
  38.         if( $this->valid() && $dbname !=  "" ) { 
  39.             if( !sybase_select_db($dbname, $this->sybdb) ) { 
  40.                 destroy(); 
  41.                 } 
  42.             } 
  43.     } 
  44.     function destroy() 
  45.     { 
  46.         if( $this->valid() ) { 
  47.             sybase_close($this->sybdb); 
  48.             } 
  49.         $this->sybdb = 0; 
  50.     } 
  51.     function valid() 
  52.     { 
  53.         return $this->sybdb != 0; 
  54.     } 
  55. }; 
  56.  
  57. class CSybRs 
  58.     var        $recs; 
  59.     var        $rows; 
  60.     var        $cols; 
  61.     var        $crow; 
  62.     var        $rarr; 
  63.     var        $fail; 
  64.  
  65.     function CSybRs($query, $sybdb) 
  66.     { 
  67.         $this->recs = 0; 
  68.         $this->rows = 0; 
  69.         $this->cols = 0; 
  70.         $this->fail = 1; 
  71.         $this->crow = 0; 
  72.  
  73.         if( !$sybdb->valid() ) return; 
  74.  
  75.         $this->recs = sybase_query($query, $sybdb->sybdb); 
  76.         if( !$this->recs ) return; 
  77.  
  78.         $this->rows = sybase_num_rows($this->recs); 
  79.         $this->cols = sybase_num_fields($this->recs); 
  80.  
  81.         $this->fail = 0; 
  82.     } 
  83.  
  84.     function destroy() 
  85.     { 
  86.         if( !$this->recs ) return; 
  87.          // sybase_freeresult($this->recs); 
  88.         $this->recs = 0; 
  89.     } 
  90.  
  91.     function valid() 
  92.     { 
  93.         return !$this->fail && $this->recs != 0; 
  94.     } 
  95.  
  96.     function seek($nrow) 
  97.     { 
  98.         if( !$this->valid() ) return 0; 
  99.         if( $this->bof() && $this->eof() ) return 0; 
  100.  
  101.         if( sybase_data_seek($this->recs, $nrow) ) { 
  102.             $this->crow = $nrow; 
  103.             $this->rarr = sybase_fetch_array($this->recs); 
  104.             if( $this->rarr ) { 
  105.                 return 1; 
  106.                 } 
  107.             } 
  108.         $this->fail = 1; 
  109.         return 0; 
  110.     } 
  111.  
  112.     function eof() 
  113.     { 
  114.         return $this->crow == $this->rows; 
  115.     } 
  116.  
  117.     function bof() 
  118.     { 
  119.         return $this->crow < 0; 
  120.     } 
  121.  
  122.     function first() 
  123.     { 
  124.         $this->crow = 0; 
  125.         if( !$this->seek($this->crow) ) { 
  126.              // mark as bof 
  127.             $this->crow = -1; 
  128.             } 
  129.     } 
  130.  
  131.     function prev() 
  132.     { 
  133.         if( $this->bof() ) return; 
  134.         $this->crow--; 
  135.         if( $this->bof() ) return; 
  136.  
  137.         if( !$this->seek($this->crow) ) { 
  138.              // mark as bof 
  139.             $this->crow = -1; 
  140.             } 
  141.     } 
  142.  
  143.  
  144.     function next() 
  145.     { 
  146.         if( $this->eof() ) return; 
  147.         $this->crow++; 
  148.         if( $this->eof() ) return; 
  149.  
  150.         $this->rarr = sybase_fetch_array($this->recs); 
  151.         if( !$this->rarr ) { 
  152.              // mark as eof 
  153.             $this->crow = $this->rows; 
  154.             } 
  155.     } 
  156.  
  157.     function last() 
  158.     { 
  159.         $this->crow = $this->rows - 1; 
  160.         if( !$this->seek($this->crow) ) { 
  161.              // mark as eof 
  162.             $this->crow = $this->rows; 
  163.             } 
  164.     } 
  165.  
  166.     function valueof($col) 
  167.     { 
  168.         if( !$this->valid() ) return; 
  169.         return $this->rarr[$col]; 
  170.     } 
  171.  
  172.     function showheader($col, $fmt =  "") 
  173.     { 
  174.         $fld = sybase_fetch_field($this->recs, $col); 
  175.         printf( "\t<th %s>%s</th>\n", $fmt, is_string($col) ? $col : $fld->name); 
  176.     } 
  177.  
  178.     function showvalue($col, $fmt =  "", $def =  " ") 
  179.     { 
  180.         $v = $this->valueof($col); 
  181.         printf( "\t<td %s>%s</td>\n", $fmt, $v ==  "" ? $def : $v); 
  182.     } 
  183.  
  184.     function display() 
  185.     { 
  186.         if( !$this->valid() ) return; 
  187.  
  188.         printf( "<table cellspacing=1 cellpadding=1 border=1>\n"); 
  189.  
  190.         printf( "<tr>\n"); 
  191.         for( $c = 0; $c < $this->cols; $c++ ) { 
  192.             $this->showheader($c); 
  193.             } 
  194.         printf( "</tr>\n"); 
  195.  
  196.         $this->first(); 
  197.         while( !$this->eof() ) { 
  198.             printf( "<tr>\n"); 
  199.             for( $c = 0; $c < $this->cols; $c++ ) { 
  200.                 $this->showvalue($c); 
  201.                 } 
  202.  
  203.             printf( "</tr>\n"); 
  204.             $this->next(); 
  205.             } 
  206.         printf( "</table>\n"); 
  207.     } 
  208. }; 
  209.  
  210. ?> 
  211.  
  212.