-1

I'm trying to understand OOP and inheritance for the code i'm cleaning up.

I have a config file

Config.php

<?php
$odb_host = "localhost";
$odb_name = "Prod";
$odb_user = "admin";
$odb_pass = "password";
?>

Main.php

class upSellCore{

    public function ociConnect($odb_user,$odb_pass,$odb_host,$odb_name)
    {
        $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$odb_host.")(PORT = 1521 )))(CONNECT_DATA=(SID=".$odb_name.")))";
        $conn = oci_connect($odb_user, $odb_pass, $db);
        if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
        else
        {
            print "ERR01"; // For PHPUnit assertTrue
        }
    }
}
$sql = "Select * from users ";

$upSell = new upSellCore();
$upSell->ociConnect($odb_user,$odb_pass,$odb_host,$odb_name);
$stid = oci_parse($upSell,$sql); // Line having issue

Since I've already initialize to call OciConnect but when i try to pass over the object to trigger oci_parse

I'm getting the following error :-

PHP Warning:  oci_parse() expects parameter 1 to be resource, object given in /I/main.php on line 46 

The $conn itself is an object from Oracle class but when i overwrite my object $upSell i can't seem to parse the connection into oci_parse.

Taken from PHPManual for end-to-end without using OOP

<?php

$conn = oci_connect('hr', 'welcome', 'localhost/XE', 'AL32UTF8');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT * FROM employees');
oci_execute($stid);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

?>
Jeeva Suriyaa
  • 96
  • 1
  • 12
  • 1
    Not sure about what you are trying to do in general, but you would need to store the return as in `$con = $upSell->ociConnect` and then use `$con` in the `oci_parse` call. – Nigel Ren Jun 10 '20 at 11:38
  • @NigelRen, ah crap it solved my problem. I keep on forgetting my oop basics. For a moment ago i thought i must leverage on construct , which i have no clue. Btw , can you post this as an answer so i can upvote it. Thanks for the explanation – Jeeva Suriyaa Jun 10 '20 at 11:46
  • There is already an answer for this. Perhaps also worth looking at https://stackoverflow.com/questions/21832809/php-singleton-database-connection-pattern for some alternative ideas. – Nigel Ren Jun 10 '20 at 11:47

1 Answers1

1

You should not be passing the class into the oci_parse function. It is expecting a connection resource. You can get the resource by calling oci_connect. In your class, your function is already doing that, therefore you can return that in the function. See below.

class upSellCore
{ 
    public function ociConnect($odb_user,$odb_pass,$odb_host,$odb_name)
    {
        $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$odb_host.")(PORT = 1521 )))(CONNECT_DATA=(SID=".$odb_name.")))";
        $conn = oci_connect($odb_user, $odb_pass, $db);

        if (!$conn) {
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        } else {
            print "ERR01"; 
        }

        return $conn; // you need to return the connection.
    }
}
$sql = "Select * from users ";

$upSell = new upSellCore();
$conn = $upSell->ociConnect($odb_user,$odb_pass,$odb_host,$odb_name); // you get the returned connection here and use it in the following line.
$stid = oci_parse($conn, $sql); // this is expecting a resource
user663976
  • 126
  • 8