How to use PHP with Oracle Wallet? [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Connect from PHP to an Oracle DB using an Oracle Wallet
We are planning to implement Oracle Wallet. It works from sqlplus as shown below.
That shows that wallet functionality is working.
export ORACLE_HOME=/afs/engg/g/lcls/package/oracle/product/11.1.0.6/client
export PATH=$ORACLE_HOME/bin:${PATH}
export TNS_ADMIN=/afs/engg/g/lcls/tools/oracle/wallets/engg_reader
$ sqlplus /#enggdev
SQL> show user
USER is "ENGG_READER"
I am so far unsuccessful to make it work from php. We have php installed with
OCI8 extension. Please guide me especially about oci_connect command and it's
syntax.
This is my php file -
<?php
// Create connection to Oracle
PutEnv("ORACLE_HOME=/afs/engg/g/lcls/package/oracle/product/11.1.0.6/client");
PutEnv("TNS_ADMIN=/afs/engg/g/lcls/tools/oracle/wallets/engg_reader");
$conn = oci_connect("/", "", "$TNS_ADMIN", null, OCI_CRED_EXT);
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!\n";
}
// Close the Oracle connection
oci_close($conn);
?>
When I execute the command $ /mccelog/package/php/php-5.4.7/bin/php connect4.php
Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong
with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and
point to the right directories in /afs/engg/u/cd/divekar/technical/connect4.php on
line 7
Note that I have properly set ORACLE_HOME and LD_LIBRARY_PATH.
Line 7 is oci_connect string which is causing that error. How to connect to
Oracle database using oci8/oci_connect ?
Thanking you in advance.
Regards.
-Shashi Divekar

THANK YOU. Your answer is useful. I tried to assign points but it's not allowing me to assign points. I did some experiements and now this works for me .
I set some environmen variables and then run the php script.
export ORACLE_HOME=/nfs/lcls/package/oracle/product/11.1.0.6/client
export TNS_ADMIN=/nfs/lcls/tools/oracle/wallets/elog_reader
I run following test php script from command line and it's working now. At lease now I have OCI8 and Oracle wallet working together.
php connect.php
<?php
// Create connection to Oracle
$conn = oci_connect("/", "", "MCCODEV", null, OCI_CRED_EXT);
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!\n";
}
// Close the Oracle connection
oci_close($conn);
?>
Thanks again for your help and for the very good information provided. I will also update my stackoverflow.com post.
Regards.
-Shashi Divekar

Related

PHP Oracle connection

I am attempting to write some connection code with PHP to a Oracle database my school is hosting.
I'm using oci_connect() at the moment to make this connection, but it is failing.
$conn = oci_connect('username', 'password', 'hostname/SID');
I can access the oracle database through sqlDeveloper, as well as phpmyadmin, so I know the login information is correct.
I checked the oracle version with select * from v$version;, it shows as 12c Enterprise.
What is wrong with my php code for connecting? Is there a better way to make an oracle connection through PHP?
This is the test code I'm running, from http://php.net/manual/en/function.oci-error.php
<?php
echo "running";
$conn = oci_connect("username", "paswwrod", "address/SID");
if (!$conn) {
$e = oci_error(); // For oci_connect errors do not pass a handle
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
echo "ending";
?>
The string "running" gets echoed, but "ending" does not, the script just stops working when it attempts oci_connect()
have you also tried including the port number to the oracle db server like so?
$conn = oci_connect("user", "pass", "localhost:1234/xe");

PHP mySQL Update doesn't work

I currently have a very big problem with PHP and mySQL. I moved a System I coded to a new Server. And while everything worked fine on the old Server, I had some problems on the new Server. Especially with mySQL. While I solved nearly all of them, I have one which I can't seem to get a hold on. And after 2 hours of trying i searched on the Internet for another two hours and updated my Syntax several times. But nothing seems to work. So now I'm here. I get a Connection to the database without a problem, but I can't update the values. I hope you can help me.
//Connect to mySQL Database
$verbindung = mysql_connect($server, $username, $passwort);
if (!$verbindung) {
echo "Couldn't connect: " . mysql_error();
}
$name=$_POST['fuehrer'];
$ident=$_POST['id'];
//Debugging
echo $name;
echo $ident;
$sql_befehl_0="UPDATE 'olgatermine' SET fuehrer = '".$name."' WHERE ID = '".$ident."';";
if (!mysql_query($verbindung, $sql_befehl_0)){
echo "Couldn't write to database";
}
//Close connection
mysql_close ( $verbindung );
What version of php use? Because in the newest versions of php the mysql functions are deprecated/removed, use instead mysqli.
Try to echo a mysqli_error at the end of the code, also mysql_error if your version of php accepts mysql functions.
If not version of php is the problem check this:
Wrong things what i see in your code..:
$sql_befehl_0="UPDATE 'olgatermine' SET fuehrer = '".$name."' WHERE ID = '".$ident."';"; // wrong
should be:
$sql_befehl_0="UPDATE `olgatermine` SET `fuehrer` = '".$name."' WHERE ID = '".$ident."';";
You need to run mysql_select_db('dbname') below line you do the mysql connection.
You can set at the first line of file:
ini_set('display_errors',1);
error_reporting(E_ALL);
to show all errors.

Can't log to oracle local DB using PHP with WebMatrix- shows an empty page

I've been trying to use the following code to connect to the oracle db from WebMatrix:
<?php
// Create connection to Oracle
$conn = oci_connect("sys","password","//localhost:1521/xe");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
For some reason, it just shows an empty page in the browser (Chrome).
Any other thing I'm trying to program using php (like echo hello world and such) are working.
What's weird that I don't even see a message of failure or success.
I'm using windows 7 64bit.
Got any idea what might be the problem ? (I've searched all over and tried many things before asking here).

Cloud 9 IDE can't connect to database

I have tried a number of things to connect to my database in cloud 9 but I keep getting similar errors.
This is my PHP code:
<?php
// Create connection
$con=mysqli_connect($IP, "$C9_USER", "", "c9");
//(host,username,password,dbname)<- guide for me
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I took the basic code from w3schools and I used it with a document explaining how cloud 9's mysql database works: https://docs.c9.io/setting_up_mysql.html
But I can't seem to connect without getting the follow error:
Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
So I'm wondering if there's a different way to change the socket to the address that cloud 9 recommended: Note: MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock
Credit to Loz Cherone
When using Cloud 9 IDE, the php variables $ID and $C9_USER mentioned in this article are not defined.
In order to retrieve these variables for use in your code, you must use the cloud 9 ide terminal by pressing ALT + T and entering:
echo $ID
echo $C9_USER
Then take those values and place them in a variable in your php code like so:
<?php
// Create connection
$IP = "value from terminal";
$C9_USER = "value from terminal";
$con=mysqli_connect($IP, $C9_USER, "", "c9");
//mysqli_connect(host,username,password,dbname); << guideline
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
SIDE NOTE: Make sure when running the mysql code that you have the data base turned on. You can turn it on by typing mysql-ctl start in the terminal.

SQLite on localhost with php doesn't work (Linux)

I am trying to get my fist sqlite programs with php working on localhost but I can't get it to work. On my machine I have installed Sqlite3 and all works fine with C/C++.
If I move created database to localhost, give read/write permissions to db file and try to access them through php I get following error message:
file is encrypted or is not a database
Here is example code I use:
<?php
$dbhandle = sqlite_open("m_test1.db", 0666, $error);
if (!$dbhandle) die ($error);
$stm = "CREATE TABLE Friends(Id integer PRIMARY KEY," .
"Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN ('M', 'F')))";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die("Cannot execute query. $error");
echo "Database Friends created successfully";
sqlite_close($dbhandle);
?>
If I run this code through browser when database don't exists then I get:
unable to open database: /var/www/m_test1.db
Info:
sqlite_libversion: 2.8.17
phpversion: 5.3.2-1ubuntu4.14
linux Ubuntu 10.04
By looking to phpinfo it seems that SQLite, SQLite3 and PDO_Sqlite is enabled.
Any help to get this working will be appreciated.
EDIT:
Solution is: 'chmod ugo+rwx /var/www' :)
After that sqlite_open and PDO both can create database.
PHP5 doesn't play nice with sqlite_open(). You'll need to use a PDO instead, like shown here: https://stackoverflow.com/a/4751965/369032
(code copied from above answer)
try
{
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:VPN0.sqlite");
echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
echo "Database loaded successfully ....";

Categories