Connect to an Oracle database using php [closed]
I need to connect to an Oracle database server with system identifier PROD, using the login "scott" and password "tiger."
Can anyone help
this is sample extract from TNSNAMES.ORA:
MYSERVICE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = tcp)(HOST = database_hostname_or_ip.com)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME=myservice)))
This is sample script to connect and execute query:
$oracledb["host"] = "MYSERVICE"; # service name in the tnsnames.ora file
$oracledb["user"] = "myuser"; # username
$oracledb["pass"] = "mypass"; # password
$oracledb["library"] = "OCI";
$connect_id = ocilogon($oracledb["user"], $oracledb["pass"], $oracledb["host"]);
$query = "SELECT * FROM table";
$statement = ociparse($connect_id, $query);
ociexecute($statement);
$result = array();
while(ocifetchinto($statement, $tmp, OCI_ASSOC + OCI_RETURN_NULLS + OCI_RETURN_LOBS))
{
array_push($result, $tmp);
}
ocifreestatement($statement);
var_dump($result); # result is here
You could use PDO to connect to oracle. That way you can also easily change between different types of databases without changes to your code, making it very portable.
But note that the Oracle driver for PDO is marked as experimental so it might be changed with later releases of PHP.
Note* Never tested PDO for oracle myself, but it's brilliant for other types of databases and makes you able to switch between different databases easily.
Use PHP function oci_connect to connecto to oracle db
精彩评论