Jump to content

PHP-MySQL-Oracle : OOP error


fdanish

Recommended Posts

Dear friends, I'm a newbie in OOP. Been given a task to convert PHP-MySQL to PHP-Oracle. DB conversion completed successfully.

 

I'm currently stuck at PHP coding. Appreciate if you could advise on what's wrong in my code as below.

 

I'm using a newly constructed class to interact with Oracle as below :

 

<?php
        //File: dbConn.php
        class dbConn {
            private $user;
            private $pswd;
            private $db;
            public  $conn;
            private $query;
            private $row;
            private $exec_mode;
            public function __construct($user, $pswd, $db,
                                        $exec_mode= OCI_COMMIT_ON_SUCCESS)
            {
                $this->user = $user;
                $this->pswd = $pswd;
                $this->db = $db;
                $this->exec_mode = $exec_mode;
                $this->GetConn();
            }
            private function GetConn()
            {
            if(!$this->conn = oci_pconnect($this->user, $this->pswd,
                                                        $this->db))
                {
                    $err = oci_error();
                        trigger_error('Failed to establish a connection: ' .
                                                            $err['message']);
                    }
                }
                public function query($sql)
                {
                    if(!$this->query = oci_parse($this->conn, $sql)) {
                        $err = oci_error($this->conn);
                        trigger_error('Failed to execute SQL query: ' .
                                                        $err['message']);
                        return false;
                    }
                    else if(!oci_execute($this->query, $this->exec_mode)) {
                        $err = oci_error($this->query);
                        trigger_error('Failed to execute SQL query: ' .
                                                            $err['message']);
                        return false;
                        }
                    return true;
                }
                public function fetch()
                {
                    if($this->row=oci_fetch_assoc($this->query)){
                        return $this->row;
                        }
                    else {
                        return false;
                        }
            }
        }
    ?>

 

In a PHP script (getUser.php), I need to call for a class which is in another script (clsRobe.php). From this clsRobe.php, an attempt to use a function in dbConn.php produced an error "Fatal error: Call to a member function query() on a non-object in '\classes\clsRobe.php'".

 

Extraction of getUser.php :

 

<?php
//File getUser.php

require_once ('../include/dbConn.php');
require_once('classes/clsRobe.php');

  $db = new dbConn($user, $pswd, $conn);
  $stfQry=$db->query("select robe_type from staff where id='" . $stfID . "'",OCI_ASSOC  + OCI_RETURN_NULLS);
  $stfAry=$db->fetch();

  $robQry=$db->query("select robe_status from robe where robe_type='" . $stfAry['ROBE_TYPE'] . "'");
  $robRow=$db->fetch();

  $robe = new clsRobe; // calling a class from clsRobe.php
  $robeAvail = $robe->getAvailableRobeSize($stfAry['ROBE_TYPE']);
.
.
?>

 

Extraction of clsRobe.php :

 

<?php
class clsRobe{
  
function getAvailableRobeSize($robeType){
    $sqlRobe="select robe_size_s, robe_size_m, robe_size_l, robe_size_xl, robe_size_xxl from robe where robe_type='" . $robeType . "'";
    $qryRobe=$db->query($sqlRobe); // error
            $rstRobe=$db->fetch($qryRobe); // error
.
.
?>

 

Please advise. Thanks.

Link to comment
Share on other sites

andrew you are getting sloppy ;)

 

change function getAvailableRobeSize($robeType)

 

to function getAvailableRobeSize($robeType, $db)

 

call it like: $robeAvail = $robe->getAvailableRobeSize($stfAry['ROBE_TYPE'], $db);

 

ignace,

 

Yezzzaaa...... it works! Thank you sooo much.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.