如何使用PHP从oracle数据库中获取数据 [英] how to fetch data from oracle database using PHP

查看:77
本文介绍了如何使用PHP从oracle数据库中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a class for executing oracle sql statements on PHP.

这是我的 index.php,我试图在其中调用我的函数

here is my index.php where I am trying to call my function

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

and my dbcontrol.php for my function

<?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() 函数不返回您可能会想到的选定行数.它返回受某些 DML 语句影响的行数(SELECT 语句除外).因此,在您的情况下,它将始终返回 0,因此条件

oci_num_rows() function does not return the number of selected rows as you might think. It returns number of rows affected by some DML statement(except SELECT statement). So in your case it will always return 0 and as a result of it the condition

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

求值为假,while 循环永远不会被执行.

evaluates to false and while loop will never be executed.

此外,oci_fetch_array() 每次取一行time 或 FALSE 如果没有更多行要返回,所以 if($this -> totalRows > 0) 在你的情况下似乎是多余的.

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.

这篇关于如何使用PHP从oracle数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆