【问题标题】:how to fetch data from oracle database using PHP如何使用 PHP 从 oracle 数据库中获取数据
【发布时间】:2013-07-20 20:37:24
【问题描述】:

我正在尝试创建一个用于在 PHP 上执行 oracle sql 语句的类。

这是我的 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'];
        }
?>

还有我的 dbcontrol.php 用于我的功能

<?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;
                }
        }
    }   
}   

?>

我不确定似乎出了什么问题。但每次我运行这个。页面上没有显示任何内容。没有结果,没有数据。但我确信数据库有数据。

【问题讨论】:

标签: php sql oracle


【解决方案1】:

总是出现空白页的原因是:

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

oci_num_rows() 函数不会像您想象的那样返回所选行数。它返回受某些 DML 语句(SELECT 语句除外)影响的行数。因此,在您的情况下,它将始终返回 0 并因此返回条件

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

评估为 false 并且永远不会执行 while 循环。

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

【讨论】:

    【解决方案2】:

    我会说,首先检查您的数据库连接,然后检查它是否与正确的数据库连接?检查选择了多少行。

    我会推荐你​​使用show_error() 函数来验证你的数据库连接。

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多