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.
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 want to move my sql database and i have exported and imported mysql.sql file from localhost to live server and now i m not getting the files and content from that database. what i do ? i did make sure connection to database if fine and successful
here's my page http://shooop23.byethost7.com
<?php
$db = mysqli_connect('','','','');
if(mysqli_connect_errno()){
echo'Database Connection Failed with following errors: '. mysqli_connect_errno();
die();
}
?>
Once you have successfully established a connection to MySQL, you need to perform a query specifying what you want to retrieve and then subsequently retrieve it.
The following example uses mysqli_fetch_row
You should explore the documentation to learn the basics.
$db = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if(mysqli_connect_errno()){
echo'Database connection failed with the following error: '.mysqli_connect_errno();
exit;
}
if ($result = mysqli_query($db, "SELECT MyCol1,MyCol2 FROM MyTable")) {
while ($row = mysqli_fetch_row($result)) {
echo"{$row[0]} - {$row[1]}<br>");
}
mysqli_free_result($result);
}
mysqli_close($db);
$db = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$sql="SELECT * FROM login";
here i connected and stored my database and set a sql command (which is now a string) into these two veriables.
if(mysqli_connect_errno()){
echo'Database connection failed with the following error: '.mysqli_connect_errno();
exit;
}
this is to check if the database is correctly connected, if not then it will show some errors.
$result = mysqli_query($db,$sql);
here i put the database and the sql command to run my query.
while($row = mysqli_fetch_array($result)){
echo $row['username'];
}
here finally outputting the usernames(username is one of the column in my table in this case) which matched with that query
i will suggest This Sql site to get a better understanding on sql queries and try to improve the secuirty because this is the basic point where hackers try to inject their attact most offenly.
Note : If your table's in the database are empty then it will not able to fetch anything
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?
Is there any way to turn on driver logging for PHP/MySQL 5? Is there a log that shows internally what's going on... like all database activity? Lower-level errors?
I'm trying to get a trivial PHP page to call a trivial stored proc on Windows 7. mysql_error isn't returning any error message when the mssql_init() fails.
Thinking it might be a permission problem I created a new user and I get the same results:
CREATE USER 'user1'#'localhost' IDENTIFIED BY 'pass1';
GRANT ALL ON *.* TO 'user1'#'localhost';
Here is the code:
create table trace (
trace varchar(50) not null,
datet datetime not null
);
drop procedure if exists mark;
delimiter !!
create procedure mark()
begin
insert into trace (trace,datet) values ('mark',now());
end; !!
delimiter ;
I can call the SP from MySQL Workbench just fine:
call mark();
Here is the PHP page:
<?php
$connection = mysql_connect('localhost', 'user1', 'pass1');
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
$sel = mysql_select_db('tw');
if (!$sel) {
die('Could not select_db: ' . mysql_error());
}
//$stmt = mssql_init('mark', $connection);
//$stmt = mssql_init('mark', $sel);
$stmt = mssql_init('mark');
if (!$stmt) {
die('Could not init: ' . mysql_error());
}
$result = mssql_execute($stmt);
if (!$result) {
die('Could not execute: ' . mysql_error());
}
mysql_free_result($result);
mssql_free_statement($stmt);
mysql_close($connection);
printf("DONE<br />");
?>
Other proof of concept pages which demonstrate insert, *select,* etc, are working just fine. Just can't get a stupid stored procedure to run from a web page!
The page output is only this:
Could not init:
(The PHP mssql_init documentation page isn't very helpful and seems to have a typo as the $link variable isn't defined.)
You're using mssql_init (Note MS SQL) to execute a stored procedure on My SQL.
Obviously the mssql_init() function can't find your Microsoft SQL Server connection 'cause you don't have one. You should only be using mysql_ functions for a MySQL connection.
And MySQL doesn't have a special function for calling stored procedures. mysql_query('CALL mark()') will work just fine.