Link to home
Start Free TrialLog in
Avatar of pradeep_bansal
pradeep_bansal

asked on

PHP and Oracle Connection

I have a xampplite software.
I am trying to connect PHP webpage with Oracle database which resides on the remote location.
I have Oracle 10.2.0 installed on my machine and connection string for that remote database is there in the tnsnames.ora.
I tried with myself but getting errors as mentioned below:-
"Warning: oci_parse() expects parameter 1 to be resource, null given in C:......"
"Warning: oci_execute() expects parameter 1 to be resource, null given in C:...."
"Warning: oci_fetch() expects parameter 1 to be resource, null given in C:...."
Avatar of cappytoi
cappytoi
Flag of Türkiye image

Are you sure the connection established? You can check the resource returned from oci_connect with:

$db_handler = oci_connect(....);
if (!$db_handler) {
 $error = oci_error();
 exit ('database connection failed with reason: '.$error);
}
Avatar of pradeep_bansal
pradeep_bansal

ASKER

Below is the code i have written for this purpose. I have mentioned the results also i am getting on the webpage

<?php
if ($c = oci_connect(......)) {
   echo "Successfully connected to Oracle.";
   oci_close($c);
 } else {
   $err = oci_error();
   echo "Oracle Connect Error " . $err['text'];
 }
function global_keyword()
{
    global $conn;
    var_dump($conn);
    oci_close($conn);  // this seems to do nothing
    var_dump($conn);
}
function global_array()
{
    var_dump($GLOBALS["conn"]);
    oci_close($GLOBALS["conn"]);  // this works
    var_dump($GLOBALS["conn"]);
}
$sql = "select * from ORACLE.PERSON_EMPLOYEE_GROUP";
   $s = oci_parse($c, $sql);
   oci_execute($s, OCI_DEFAULT);
   while (oci_fetch($s)) {
   echo "COL1 = " . oci_result($s, "COL1") .
   ", COL2 = " . oci_result($s, "COL2") . "<br>\n";
}
oci_close($c);
?>

Results:-

Successfully connected to Oracle.
Warning: oci_parse() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 30

Warning: oci_execute() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 31

Warning: oci_fetch() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 32

Warning: oci_close() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 38
Please ignore above comment
-------------------------------------
Below is the code i have written for this purpose. I have mentioned the results also i am getting on the webpage
------------------------------------------------------------------
<?php
if ($c = oci_connect(......)) {
   echo "Successfully connected to Oracle.";
   oci_close($c);
 } else {
   $err = oci_error();
   echo "Oracle Connect Error " . $err['text'];
 }
$sql = "select * from ORACLE.PERSON_EMPLOYEE_GROUP";
   $s = oci_parse($c, $sql);
   oci_execute($s, OCI_DEFAULT);
   while (oci_fetch($s)) {
   echo "COL1 = " . oci_result($s, "COL1") .
   ", COL2 = " . oci_result($s, "COL2") . "<br>\n";
}
oci_close($c);
?>
-----------------------------------------------------------------
Results:-
-----------------------------------------------------------------
Successfully connected to Oracle.
Warning: oci_parse() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 30

Warning: oci_execute() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 31

Warning: oci_fetch() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 32

Warning: oci_close() expects parameter 1 to be resource, null given in C:\xampplite\htdocs\xampp\MonitoringDashboard\Copy of testnewConn.php on line 38
ASKER CERTIFIED SOLUTION
Avatar of cappytoi
cappytoi
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Cappytoi,

It worked i just missed out this part.

Thanks