Monday, February 10, 2014

Using Oracle with PHP and CodeIgniter

Using Oracle with PHP and CodeIgniter



Step 1:

Install OCI & enable OCI8 in WAMP(Please click How to Install & enable oci8 in WAMP)


Step 2: Configuration database.php


CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located atapplication/config/database.php. You can also set database connection values for specific environments by placing database.php it the respective environment config folder.
The config settings are stored in a multi-dimensional array with this prototype:


$db['pblmig']['hostname'] = '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.200.200)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME =pblmig )))';
$db['pblmig']['username'] = 'cib';
$db['pblmig']['password'] = 'cib';
$db['pblmig']['database'] = 'pblmig';
$db['pblmig']['dbdriver'] = 'oci8';
$db['pblmig']['dbprefix'] = '';
$db['pblmig']['pconnect'] = FALSE;
$db['pblmig']['db_debug'] = TRUE;
$db['pblmig']['cache_on'] = FALSE;
$db['pblmig']['cachedir'] = '';
$db['pblmig']['char_set'] = 'utf8';
$db['pblmig']['dbcollat'] = 'utf8_general_ci';
$db['pblmig']['swap_pre'] = '';
$db['pblmig']['autoinit'] = TRUE;
$db['pblmig']['stricton'] = FALSE;


Step 3: Data Fetch from Oracle & Show

Controllers


Create a controller db_controller.php file which in CI/application/controllers location

<?php

class db_controller extends CI_Controller{
   
   function __construct(){
        parent::__construct();
    }


public function db_data(){
       
$this->load->model('db_model');
                                                               
$data[dtl]=$this->db_model->get_data( );
$data[row]=$this->db_model->get_tot_data();
$this->load->view('pages/show_data', $data);
                               

 
}


}

?>

Model

Create a  model db_model.php file which in CI/application/models  location


<?php

class db_model extends CI_Controller{
   
   function __construct(){
        parent::__construct();
    }


public function get_data(){

$this->pblmig_db = $this->load->database('pblmig', true);

 $sql="SELECT  CUST_NAME FROM CUSTOMER “;

$stmt = oci_parse($this->pblmig_db->conn_id, $sql);

oci_execute($stmt);
$row = oci_fetch_all($stmt, $result);
oci_free_statement($stmt);
oci_close($this->pblmig_db->conn_id);
return $result;
   }      
                       
// Return no of row
public function get_tot_data(){
 $this->pblmig_db = $this->load->database('pblmig', true);

 $sql="SELECT  CUST_NAME FROM CUSTOMER “;

$stmt = oci_parse($this->pblmig_db->conn_id, $sql);

oci_execute($stmt);
$row = oci_fetch_all($stmt, $result);
oci_free_statement($stmt);
oci_close($this->pblmig_db->conn_id);
return $row;
   }      

           

}

?>


VIEW

Create a page show_data.php file which in CI/application/views/pages location

<table>
                                                           
<tr>
<td style="FONT-SIZE: 12px;text-align:center">
</td>
</tr>
<?
for ($i = 0; $i <$row; $i++) {
?>
<tr>
<td style="FONT-SIZE: 12px;text-align:left">                                                  
</td>
<?        foreach ($dtl as $key=>$data) {           ?>
            <td style="FONT-SIZE: 12px;text-align:left">
            <?
               echo $data[$i];
            ?>
</td>
            <?       
            }
            ?>
</tr>   
                                                                                                 
<?
}
                                   
?>
</table>


 Now, test your application. Point your browser using this URL

http://localhost/CI/index.php/db_data / get_data                                   

1 comment: