sql - how to fetch data from oracle database using PHP -
i trying create class executing oracle sql statements on php.
here index.php trying call function
<?php include "dbaseconn/dbcontrol.php"; $dbcontrol = new dbcontrol; $dbcontrol->execute(" select * sample_table"); foreach($dbcontrol->data $items) { echo $items['sample_column_name']; } ?>
and dbcontrol.php 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 statement $this -> execute = oci_execute($this -> statement); //execute 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 seems wrong. everytime run this. nothing shown on page. no result, no data. sure database has data.
the reasons why keep getting blank page are:
1. $this -> totalrows = oci_num_rows($this -> statement);
oci_num_rows() function not return number of selected rows might think. returns number of rows affected dml statement(except select statement). in case return 0 , result of condition
2. if($this -> totalrows > 0)
evaluates false , while
loop never executed.
besides, oci_fetch_array() fetches 1 row @ time or false if there no more rows return, if($this -> totalrows > 0)
in case seems redundant.
Comments
Post a Comment