0

Hey guys im trying to connect to an oracle database with php. I tried it like i do it with mysql. How to do it like this:

$host="localhost";
$user="username";
$pass="password";
$db="database";

$link = mysql_connect($host, $user, $pass) or die ("Keine Verbindung zu der Datenbank moeglich.");

mysql_select_db($db, $link);

$sql = "SQL query goes here";

$result = mysql_query($sql);

How can i do exact this with an oracle database. I have the following connection details sid, ip, port, username, password.

5
  • You cannot use the MySQL extension to connect to Oracle. Period. As the name suggests, it's built for MySQL. Commented Jan 15, 2013 at 10:30
  • He just gave an example how he's doing it in mysql. Nothing wrong with that. Commented Jan 15, 2013 at 10:32
  • @AlenOblak - Well, he said he actually tried that code... Commented Jan 15, 2013 at 10:38
  • @ÁlvaroG.Vicario this is only example how i do it in mysql Commented Jan 15, 2013 at 10:39
  • Recommended reading: The Underground PHP and Oracle Manual Commented Jan 15, 2013 at 10:41

3 Answers 3

4

Simple script:

$DB = '//1.2.3.4:1521/XE';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_CHAR = 'AL32UTF8';

$conn = oci_connect($DB_USER, $DB_PASS, $DB, $DB_CHAR);
$statement = oci_parse($conn, 'select 1 from dual');
oci_execute($statement);
$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
Sign up to request clarification or add additional context in comments.

5 Comments

And how to use the sid in the script without it i can't connect and for what stands the /xe for behind the ip
This is the only recommended way to connect to Oracle. The PDO driver is tagged as experimental because it's basically abandoned.
The "xe" after the IP is the SID.
OK your script gives me this error: Fatal error: Call to undefined function oci_connect()
You need the OCI extension on your server. Check the documentation: si.php.net/manual/en/oci8.setup.php
1

To connect with an Oracle database, you don't use the mysql extension (since that is for MySQL). You should use PDO, with the OCI/Oracle adapter.

Comments

0

You'll want to use PDO to connect to Oracle here is the PHP manual page on creating a connection using PDO, the example given is for MySQL but it will work fine with Oracle. You will need to ensure the PDO:Oracle extension is installed and running on your PHP configuration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.