问题
I have an Oracle database set up on Amazon RDS and am trying to connect to it in PHP.
I am fairly sure my connection string is correct because I connected using sqlplus and am able to retrieve data. The server is an Amazon Linux server, however I have also tried to connect using a different server and get the error:
Is there any more set up that I need to do in either the server or the database server, I have just started using AWS and am not sure if anything else needs to be done.
ORA-12154: TNS:could not resolve the connect identifier specified... Line 19.
Here is my test code:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo "OCI Test<br>";
$tns = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xyz.abc.us-west-2.rds.amazonaws.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));";
echo "<pre>$tns</pre>\n";
$username = "xxx";
$password = "yyy";
$db = @oci_connect($username, $password, $tns);
if (!$conn)
{
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$sql = "select * from city";
$stmt = OCIParse($db, $sql);
if(OCIExecute($stmt))
{
while(OCIFetchInto($stmt, $row, OCI_RETURN_NULLS))
{
echo $row[0]. "-" . $row[1]."<br>";
}
}
OCIFreeStatement($stmt);
?>
</body>
</html>
Edit: Also the Amazon Linux server just shows nothing, while another server that I have access to gives errors, is there a configuration change for this?
回答1:
The $tns variable looks wrong.
Try
$tns = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xyz.abc.us-west-2.rds.amazonaws.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));";
来源:https://stackoverflow.com/questions/35933251/cannot-connect-my-rds-oracle-instance-to-a-php-server