"Fossies" - the Fresh Open Source Software Archive

Member "mythreads/lib/phplib/db_odbc.inc" (29 Mar 2001, 4830 Bytes) of package /linux/privat/mythreads-links_1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) fasm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "db_odbc.inc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.0_vs_1.2.1.

    1 <?php
    2 /*
    3  * Session Management for PHP3
    4  *
    5  * Copyright (c) 1998-2000 Cameron Taggart (cameront@wolfenet.com)
    6  *        Modified by Guarneri carmelo (carmelo@melting-soft.com)
    7  *                   
    8  * $Id: db_odbc.inc,v 1.1 2001-03-29 21:51:52 ldrolez Exp $
    9  */ 
   10 
   11 class DB_Sql {
   12   var $Host     = "";
   13   var $Database = "";
   14   var $User     = "";
   15   var $Password = "";
   16   var $UseODBCCursor = 0;
   17 
   18   var $Link_ID  = 0;
   19   var $Query_ID = 0;
   20   var $Record   = array();
   21   var $Row      = 0;
   22   
   23   var $Errno    = 0;
   24   var $Error    = "";
   25 
   26   var $Auto_Free = 0;     ## set this to 1 to automatically free results
   27 
   28   /* public: constructor */
   29   function DB_Sql($query = "") {
   30       $this->query($query);
   31   }
   32 
   33   function connect() {
   34     if ( 0 == $this->Link_ID ) {
   35       $this->Link_ID=odbc_pconnect($this->Database, $this->User, $this->Password, $this->UseODBCCursor);
   36       if (!$this->Link_ID) {
   37         $this->halt("Link-ID == false, odbc_pconnect failed");
   38       }
   39     }
   40   }
   41   
   42   function query($Query_String) {
   43 
   44     /* No empty queries, please, since PHP4 chokes on them. */
   45     if ($Query_String == "")
   46       /* The empty query string is passed on from the constructor,
   47        * when calling the class without a query, e.g. in situations
   48        * like these: '$db = new DB_Sql_Subclass;'
   49        */
   50       return 0;
   51 
   52     $this->connect();
   53     
   54 #   printf("<br>Debug: query = %s<br>\n", $Query_String);
   55 
   56 #   rei@netone.com.br suggested that we use this instead of the odbc_exec().
   57 #   He is on NT, connecting to a Unix MySQL server with ODBC. -- KK
   58 #    $this->Query_ID = odbc_prepare($this->Link_ID,$Query_String);
   59 #    $this->Query_Ok = odbc_execute($this->Query_ID);
   60         
   61     $this->Query_ID = odbc_exec($this->Link_ID,$Query_String);
   62     $this->Row = 0;
   63     odbc_binmode($this->Query_ID, 1);
   64     odbc_longreadlen($this->Query_ID, 4096);
   65     
   66     if (!$this->Query_ID) {
   67       $this->Errno = 1;
   68       $this->Error = "General Error (The ODBC interface cannot return detailed error messages).";
   69       $this->halt("Invalid SQL: ".$Query_String);
   70     }
   71     return $this->Query_ID;
   72   }
   73   
   74   function next_record() {
   75     $this->Record = array();
   76     $stat      = odbc_fetch_into($this->Query_ID, ++$this->Row, &$this->Record);
   77     if (!$stat) {
   78       if ($this->Auto_Free) {
   79         odbc_free_result($this->Query_ID);
   80         $this->Query_ID = 0;
   81       };
   82     } else {
   83       // add to Record[<key>]
   84       $count = odbc_num_fields($this->Query_ID);
   85       for ($i=1; $i<=$count; $i++)
   86         $this->Record[strtolower(odbc_field_name ($this->Query_ID, $i)) ] = $this->Record[ $i - 1 ];
   87     }
   88     return $stat;
   89   }
   90   
   91   function seek($pos) {
   92     $this->Row = $pos;
   93   }
   94 
   95   function metadata($table) {
   96     $count = 0;
   97     $id    = 0;
   98     $res   = array();
   99 
  100     $this->connect();
  101     $id = odbc_exec($this->Link_ID, "select * from $table");
  102     if (!$id) {
  103       $this->Errno = 1;
  104       $this->Error = "General Error (The ODBC interface cannot return detailed error messages).";
  105       $this->halt("Metadata query failed.");
  106     }
  107     $count = odbc_num_fields($id);
  108     
  109     for ($i=1; $i<=$count; $i++) {
  110       $res[$i]["table"] = $table;
  111       $name             = odbc_field_name ($id, $i);
  112       $res[$i]["name"]  = $name;
  113       $res[$i]["type"]  = odbc_field_type ($id, $name);
  114       $res[$i]["len"]   = 0;  // can we determine the width of this column?
  115       $res[$i]["flags"] = ""; // any optional flags to report?
  116     }
  117     
  118     odbc_free_result($id);
  119     return $res;
  120   }
  121   
  122   function affected_rows() {
  123     return odbc_num_rows($this->Query_ID);
  124   }
  125   
  126   function num_rows() {
  127     # Many ODBC drivers don't support odbc_num_rows() on SELECT statements.
  128     $num_rows = odbc_num_rows($this->Query_ID);
  129     //printf ($num_rows."<br>");
  130 
  131     # This is a workaround. It is intended to be ugly.
  132     if ($num_rows < 0) {
  133       $i=10;
  134       while (odbc_fetch_row($this->Query_ID, $i)) 
  135         $i*=10;
  136 
  137       $j=0;
  138       while ($i!=$j) {
  139         $k= $j+intval(($i-$j)/2);
  140         if (odbc_fetch_row($this->Query_ID, $k))
  141           $j=$k;
  142         else 
  143           $i=$k;
  144         if (($i-$j)==1) {
  145           if (odbc_fetch_row($this->Query_ID, $i)) 
  146             $j=$i;
  147           else 
  148             $i=$j; 
  149         };
  150         //printf("$i $j $k <br>");
  151       };
  152       $num_rows=$i;
  153     }
  154 
  155     return $num_rows;
  156   }
  157   
  158   function num_fields() {
  159     return count($this->Record)/2;
  160   }
  161 
  162   function nf() {
  163     return $this->num_rows();
  164   }
  165   
  166   function np() {
  167     print $this->num_rows();
  168   }
  169   
  170   function f($Field_Name) {
  171     return $this->Record[strtolower($Field_Name)];
  172   }
  173   
  174   function p($Field_Name) {
  175     print $this->f($Field_Name);
  176   }
  177   
  178   function halt($msg) {
  179     printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  180     printf("<b>ODBC Error</b>: %s (%s)<br>\n",
  181       $this->Errno,
  182       $this->Error);
  183     die("Session halted.");
  184   }
  185 }
  186 ?>