简体   繁体   English

如何使用PHP从Oracle数据库中获取数据

[英]how to fetch data from oracle database using PHP

I am trying to create a class for executing oracle sql statements on PHP. 我正在尝试创建一个用于在PHP上执行oracle sql语句的类。

here is my index.php where I am trying to call my function 这是我的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'];
        }
?>

and my dbcontrol.php for my function 和我的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;
                }
        }
    }   
}   

?>

I'm not sure what seems to be wrong. 我不确定似乎有什么问题。 But everytime I run this. 但是每次我运行这个。 Nothing is shown on page. 页面上未显示任何内容。 No result, No data. 没有结果,没有数据。 But I am sure that database has data. 但是我确信数据库中有数据。

The reasons why you are keep getting a blank page are: 您总是得到空白页的原因是:

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

oci_num_rows() function does not return the number of selected rows as you might think. oci_num_rows()函数不会返回您可能认为的所选行数。 It returns number of rows affected by some DML statement(except SELECT statement). 它返回受某些DML语句(SELECT语句除外)影响的行数。 So in your case it will always return 0 and as a result of it the condition 因此,在您的情况下,它将始终返回0,因此条件

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

evaluates to false and while loop will never be executed. 计算结果为false,并且while循环将永远不会执行。

Besides, oci_fetch_array() fetches one row at a time or FALSE if there is no more rows to return, so if($this -> totalRows > 0) in your case seems redundant. 此外, oci_fetch_array()一次获取一行,如果没有更多行要返回, if($this -> totalRows > 0) FALSE,因此在您的情况下if($this -> totalRows > 0)似乎是多余的。

I would say, first check with your database connectivity, then check, whether it is connected with right database or not ? 我会说,首先检查您的数据库连接性,然后检查它是否与正确的数据库连接? Check for how many rows is being selected. 检查选择了多少行。

I will recommend you to use show_error() function to validate your database connection. 我建议您使用show_error()函数来验证数据库连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM