2

I am trying to create a class for executing oracle sql statements on 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'];
        }
?>

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.

2

2 Answers 2

1

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. 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) 

evaluates to false and while loop will never be executed.

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.