2013-07-22 104 views
1

我想创建一个用于在PHP上执行oracle sql语句的类。如何使用PHP从oracle数据库获取数据

这里是我的index.php这里我想打电话给我的功能

<?php 
    include "dbaseconn/dbcontrol.php"; 
    $DbControl = new DbControl; 
    $DbControl->execute(" SELECT * FROM SAMPLE_TABLE"); 
     foreach($DbControl->data as $items) 
     { 
     echo $items['SAMPLE_COLUMN_NAME']; 
     } 
?> 

和我为我的功能

<?php 
class DbControl{ 
    public $dbstr ='(DESCRIPTION = 
       (ADDRESS_LIST = 
          (ADDRESS = (COMMUNITY = tcp.world)(PROTOCOL = TCP)(HOST = XX.XXX.XXX.XX)(PORT = XXXX)) 
        ) 
        (CONNECT_DATA = 
         (SID = XXXXX) 
        ) 
       )'; 

     public $user = "XXXXX"; 
     public $password = "XXXXX"; 

     function connect(){ 
      $this->connection = oci_connect($this->user,$this->password,$this->dbstr) or die(oci_error()); 
     } 

    function execute($query){ 
     $this -> connect(); //Database Connect 
     $this -> statement = oci_parse($this->connection,$query); //prepare the statement 
     $this -> execute = oci_execute($this -> statement); //execute the statement 
     $this -> totalRows = oci_num_rows($this -> statement); //get total number of rows 
     $this -> data = array(); 
     if($this -> totalRows > 0){ 
          //fetch data 
       while($result = oci_fetch_array($this->statement)){ 
        $this -> data[] = $result; 
       } 
     } 
    } 
} 

?> 

dbcontrol.php我不知道什么似乎是错的。但每次我运行这个。页面上没有显示任何内容。没有结果,没有数据。但我确信数据库有数据。

+0

我发现这个链接很有用http://st-curriculum.oracle.com/obe/db/11g/r2/prod/appdev/opensrclang/phphol2010_db/php_db.htm – Microtechie

+0

为什么不使用PDO?它可以处理任何类型的数据库,包括oracle afaik –

回答

0

你为什么总是收到一个空白页面的原因是:

1. $this -> totalRows = oci_num_rows($this -> statement); 

oci_num_rows()函数不返回选定的行数,你可能会认为。它返回受某些DML语句影响的行数(SELECT语句除外)。因此,在你的情况下,它总是会返回0,并作为其结果的条件

2. if($this -> totalRows > 0) 

计算结果为假,while循环将永远不会被执行。

此外,oci_fetch_array()一次只读取一行,或者如果没有更多行要返回,则为FALSE,因此if($this -> totalRows > 0)在您的情况下似乎是多余的。

0

我想说,首先检查你的数据库连接,然后检查它是否与正确的数据库连接?检查有多少行正在被选中。

我会建议你使用show_error()函数来验证你的数据库连接。

相关问题