你如何从PHP连接到oracle db

问题描述:

I am trying to connec to oracle table from php. I have odbc configured and tested that it works. I have the following php file that I created to test the connection but I get

This is the code:

<html>
<body>

<?php
$conn=odbc_connect('<dsn name>','<username>','<password>');
if (!$conn) {exit("Connection Failed: " . $conn);}
$sql="SELECT T."Node" from <table name> T";
$rs=odbc_exec($conn,$sql);
if (!$rs) {exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
{
    $compname=odbc_result($rs,"CompanyName");
    $conname=odbc_result($rs,"ContactName");
    echo "<tr><td>$compname</td>";
    echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>

</body>
</html> 

My version :

C:\PHP>php --version
PHP 5.3.26 (cli) (built: Jun  5 2013 19:16:29)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies

I configured error on on the php.ini file and I am getting this error:

Parse error: syntax error, unexpected T_STRING in C:\inetpub\wwwroot\test.php on line 8

it is complaining about the sql line. it does not like this T."Node" ( the double quotes). Is it possible to escape the double quotes within the sql line in php. How would I address this?

sql="SELECT T."Node" from <table name> T";

must be

sql="SELECT T" . $node. " from <table name> T";

if $node is a var,

sql="SELECT T.\"Node\" from <table name> T";

otherwise to escape the quotes.

You might want to use some editor with syntax and error highlighting, e.g. netbeans, eclipse, notepad++...

You can get rid of the quotes around the table. Those are not needed.

$sql="SELECT T.Node from <table name> T";

There is a syntax error here:

$sql="SELECT T."Node" from T";

use literal string (single quotes) if you want to use quotes inside the string:

$sql='SELECT T."Node" from T';