I simply don't get it.
I can connect fine to the Oracle database, but my account can only execute stored procedures. So I try to use one.
echo "before";
$nrows = '';
$stid = oci_parse($conn, 'begin :r := AR_INTEGRATIONS.F_SRVCIMPROVE_ACCNT_INCS(:p); end');
oci_bind_by_name($stid, ':p', '');
oci_bind_by_name($stid, ':r', $nrows);
if(!oci_execute($stid)){
$e = oci_error();
print htmlentities($e['message']);
exit;
}
echo "<br/>After";
When I load this on my browser, it's blank. But when I comment out the oci_bind_by_name() lines, it displays before but not after.
This is the first time I'm using PHP to connect to Oracle and execute queries. It's very different from just using MySQL.
What's going on?
Related
I'm having some problem with accessing Oracle database records through PHP - OCI8 functions
So I am trying to echo any record in PHP from my Oracle database to my page.
<?php
$conn = oci_connect('SYSTEM', '1234', 'localhost/orcl');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}else{
echo "Successfully connected!";
}
$sql = 'SELECT * FROM userPage';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) {
echo $row[0];
echo $row[1];
}
oci_free_statement($stid);
oci_close($conn);
?>
I am not getting any error back so my connection is valid and also my query is working well on my table because if I change the name of the table in the query I am getting an error that my table does not exist.
I have almost tried everything from oci8 functions and all my result is a blank page.
In Oracle I have created the table correctly and trying to access the data through SYSTEM so I should't have privilige problems.
I am using Oracle 12c, instant client 12_2 windows 7 operating system (didn't work on win10 at all), XAMPP v3.2.4.
Any ideas would help a lot. Thanks for reading.
I am trying to fetch data from Oracle in PHP on WAMP Server with the following code but, the data is not getting fetched, neither am i getting any error. But when i execute the same query on Oracle directly, i am getting the data. Also, with same connection parameters within the same php file, i am able to fetch data for another query
$server = "localhost";
$sid = "xe";
$user = "hrs";
$passwd = "hrs123";
$conn = oci_connect($user, $passwd, $server."/".$sid);
if (!$conn) {
$e = oci_error();
echo $m['message'], "\n";
exit;
}
else{
}
$staffno = test_input($_POST['field1']);
if($staffno!='')
{
$exempquery="select stno as stno, nvl(ffname,'')||' '||nvl(lname,'') as fname, substr(gradep,0,1) as grd, nvl(decode(sex,'M','MALE','F','FEMALE'),'') as sex, to_char(birth_dt,'dd-mm-yyyy') as birthdt, to_char(sep_dt,'dd-mm-yyyy') as sepdt, to_char(ret_dt,'dd-mm-yyyy') as retdt, sepdes from emp_master where stno='$staffno' and ((sep_dt<='31-03-2013' and ret_dt<='31-03-2013') or (sep_dt is null and ret_dt<='31-03-2013'))";
$exempstid=oci_parse($conn,$exempquery);
$exempchk=oci_execute($exempstid);
$exemprow=oci_fetch_array($exempstid, OCI_BOTH);
$name=$exemprow['FNAME'];
$grd=$exemprow['GRD'];
$sex=$exemprow['SEX'];
$birthdt=$exemprow['BIRTHDT'];
$sepdt=$exemprow['SEPDT'];
$retdt=$exemprow['RETDT'];
$sepdes=$exemprow['SEPDES'];
}
The database connection is working fine.
Any help would be highly appreciated.
Check your webserver log files for errors
During development, you could do worse than add this to the top of the script to make sure errors are shown:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Remember to remove these where you put your script in productions
Consider using a HEREDOC or NOWDOC for complex SQL statements, see PHP 5.3 "NOWDOCS" make SQL escaping easier.
Use OCI_ASSOC instead of the (default) OCI_BOTH.
Before going too far, you MUST rewrite your SQL statement to use a bind variable because the way you concatenate $staffno into the statement is a security risk (and will affect performance & scalability). I don't have your data, but something like this is the way to go:
$exempquery="select stno as stno, nvl(ffname,'')||' '||nvl(lname,'') as fname, substr(gradep,0,1) as grd, nvl(decode(sex,'M','MALE','F','FEMALE'),'') as sex, to_char(birth_dt,'dd-mm-yyyy') as birthdt, to_char(sep_dt,'dd-mm-yyyy') as sepdt, to_char(ret_dt,'dd-mm-yyyy') as retdt, sepdes from emp_master where stno = :staffnobv and ((sep_dt<='31-03-2013' and ret_dt<='31-03-2013') or (sep_dt is null and ret_dt<='31-03-2013'))";
$s = oci_parse($c, $exempquery);
if (!$s) {
$m = oci_error($c);
trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
}
$r = oci_bind_by_name($s, ":staffnobv", $staffno);
if (!$r) {
$m = oci_error($s);
trigger_error('Could not bind a parameter: '. $m['message'], E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
$m = oci_error($s);
trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
}
You possibly want one or more bind variables for the date too, unless it really will never, ever change.
I'm trying to query a remote Oracle database through a PHP script. I'm running WAMP server. The Oracle database is read only. I have no problem connecting using the PHP script but I get errors on the oci_execute command.
This is the script I use:
<?php
$c = oci_connect("username", "password", "oracle_SID");
if (!$c) {
$e = oci_error();
trigger_error('Could not connect to database: '. $e['message'],E_USER_ERROR);
}
$s = oci_parse($c, 'Select * from fdma.t_title_stage');
if (!$s) {
$e = oci_error($c);
trigger_error('Could not parse statement: '. $e['message'], E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
$e = oci_error($s);
trigger_error('Could not execute statement: '. $e['message'], E_USER_ERROR);
}
oci_free_statement($stid);
oci_close($conn);
?>
These are the errors I'm getting when I run the script:
If the database is read-only I should be able to run a select * query against it, right?
Oracle has no concept of a database being "read-only". The user you're connecting with may not have any create/insert/update/delete rights, which would make the database read-only to this user, but it's a property of the user, not the database.
The error you're getting, in conjunction with the sql statement (... from fdma.t_title_stage) seems like you're connecting with a user that doesn't even have select rights on fdma's t_title_stage table. Try loggin in as fdma, the grant select on t_title_stage to xxx with xxx being the username you're using in your oci_connect statement.
I have a script with the following code to insert data into an Oracle table:
$sql = "INSERT INTO EXAMPLE_TABLE (ID, FIELD, VAL) VALUES (:id, :test, :ok)";
$stid = oci_parse($conn, $sql);
//die("parsed ok");
oci_bind_by_name($stid, ":id", $id);
oci_bind_by_name($stid, ":test", $test);
oci_bind_by_name($stid, ":ok", $ok);
//die("params bound");
$result =#oci_execute($stid);
if(!$result)
{
$err = oci_error($stid);
$error = $err['message'];
echo $error; die('end error');
}
echo oci_num_rows($stid) . " rows inserted.<br />";
die("inserted");
The script was working fine, and then it just stopped. I threw in some die statements to find where the script is stalling. The last place I can get to is "die("params bound")".
I've verified that the variables are ok and that the database connection is valid. I also manually connected to the database using sqldeveloper and had no problem.
Why is this script suddenly stalling?
As often happens, I had a though immediately after I posted this question. I checked to see how many connections I had open in a GUI, and then closed the connections.
It turns out that I had some uncommitted changes from some queries that I ran manually in my GUI, and I think that was stalling the script at oci_execute. After rolling back the changes and restarting my connection, the script works fine now.
I am using wamp server 2.0 working with PHP and my database is Oracle 10g.
I am new to php and I am try to fetch data from database.
There are two columns in my table. I want show 1 column data.
After executing I get only blank page, without data from database (and yes, there is data in my database).
How can I fix this?
<?php
$c = oci_connect("system", "123", "localhost/XE");
if (!$c) {
echo "Unable to connect: " . var_dump( oci_error() );
die();
}
$s = oci_parse($c, "select col2 from tab1");
oci_execute($s, OCI_DEFAULT);
while ($row =oci_fetch($s)) {
echo $row['name']."<br>";
}
// Commit to save changes...
oci_commit($c);
// Logoff from Oracle...
oci_free_statement($s);
oci_close($c);
?>
oci_fetch copies a result into an internal buffer that you have to access e.g. using oci_result.
BTW: Had you set a higher value for error_reporting and turned on display_errors, then you would have noticed, that you tried to treat a boolean as an array when printing $row['name'].