php连oracle的类文件[1]

[入库:2005年8月28日] [更新:2007年3月24日]

本文简介:

 putenv("oracle_sid=oracle8");
  putenv("oracle_home=/oracle/product/release/8.1.5");
  
  class db_sql {
  var $debug = 0;
  var $sqoe = 1; // sqoe= show query on error
  
  var $database = "dev";
  var $user = "scott";
  var $password = "tiger";
  
  var $link_id = 0;
  var $record = array();
  var $row;
  var $numrow;
  var $parse;
  var $error = "";
  
  function connect() {
  if ( 0 == $this->link_id ) {
   if($this->debug) {
   printf("
connecting to $this->database...
\n");
   }
   $this->link_id=ocilogon("$this->user","$this->password");
  
   if (!$this->link_id) {
   $this->halt("link-id == false " .
   "($this->link_id), ocilogon failed");
   }
  
   if($this->debug) {
   printf("
obtained the link_id: $this->link_id
\n");
   }
   }
   }
  
   function query($query_string) {
   if($query_string=="")
   {
   echo "执行语句不能为空!";
   return false;
   }
   $this->connect();
  
   $this->parse=ociparse($this->link_id,$query_string);
  
   if(!$this->parse) {
   $this->error=ocierror($this->parse);
   } else { ociexecute($this->parse);
   $this->error=ocierror($this->parse);
   }
  
   $this->row=0;
  
   if($this->debug) {
   printf("debug: query = %s
\n", $query_string);
   }
  
   if ($this->error["code"]!=1403 && $this->error["code"]!=0 && $this->sqoe)
   echo "
  ".$this->error["message"]."
  query :\"$query_string\"";
   $numrow=ocirowcount($this->parse);
   return $this->parse;
  
   }
  
   function next_record() {
   if(0 == ocifetchinto($this->parse,$result,oci_assoc+oci_return_nulls)) {
   if ($this->debug) {
   printf("
id: %d,rows: %d
\n",
   $this->link_id,$this->num_rows());
   }
   $this->row +=1;
  
   $errno=ocierror($this->parse);
   if(1403 == $errno) { # 1043 means no more records found
   $this->error="";
   $this->disconnect();
   $stat=0;
   } else {
   $this->error=ocierror($this->parse);
   if($this->debug) {
   printf("
error: %s",
   $this->error["message"]);
   }
   $stat=0;
   }
   } else {
   for($ix=1;$ix<=ocinumcols($this->parse);$ix++) {
   $col=strtoupper(ocicolumnname($this->parse,$ix));
   $colreturn=strtolower($col);
   $this->record[ "$colreturn" ] = $result["$col"];
   if($this->debug) echo"[$col]:".$result["$col"]."
\n";
   }
   $stat=1;
   }
  
   return $stat;
   }
   function record_exist() {
   if(0 == ocifetchinto($this->parse,$result)) {
   return 0;
   } else {
   return 1;
   }
  
   return $stat;
   }
  
   function seek($pos) {
   $this->row=$pos;
   }
  
   function metadata($table,$full=false) {
   $count = 0;
   $id = 0;
   $res = array();
  
   /*
   * due to compatibility problems with table we changed the behavior
   * of metadata();
   * depending on $full, metadata returns the following values:
   *
   * - full is false (default):
   * $result[]:
   * [0]["table"] table name
   * [0]["name"] field name
   * [0]["type"] field type
   * [0]["len"] field length
   * [0]["flags"] field flags ("not null", "index")
   * [0]["format"] precision and scale of number (eg. "10,2") or empty
   * [0]["index"] name of index (if has one)
   * [0]["chars"] number of chars (if any char-type)
   *
   * - full is true
   * $result[]:
   * ["num_fields"] number of metadata records
   * [0]["table"] table name
   * [0]["name"] field name
   * [0]["type"] field type
   * [0]["len"] field length
   * [0]["flags"] field flags ("not null", "index")
   * [0]["format"] precision and scale of number (eg. "10,2") or empty
   * [0]["index"] name of index (if has one)
   * [0]["chars"] number of chars (if any char-type)
   * ["meta"][field name] index of field named "field name"
   * the last one is used, if you have a field name, but no index.
   * test: if (isset($result['meta']['myfield'])) {} ...
   */
  
   $this->connect();
  
   ## this is a right outer join: "(+)", if you want to see, what
   ## this query results try the following:
   ## $table = new table; $db = new my_db_sql; # you have to make
   ## # your own class
   ## $table->show_results($db->query(see query vvvvvv))
   ##
   $this->query("select t.table_name,t.column_name,t.data_type,".
   "t.data_length,t.data_precision,t.data_scale,t.nullable,".
   "t.char_col_decl_length,i.index_name".

本文关键:php连oracle的类文件
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top