Reputation: 64
I've found an issue while working with php ODBC functions. odbc_fetch_array and odbc_fetch_object fail when query has a join with a table that has the same column name, even when that certain field is excluded from the field selection:
for example
$con = odbc_connect("Driver={SQL Server Native Client 11.0};Server=$serverName;Database=$db;", 'user', 'pass',SQL_CUR_USE_ODBC);
$query="SELECT table1.field1, table1.field2 from table1 JOIN table2 ON table1.field1=table2.field1";
$result = odbc_exec($con,$query);
$a=odbc_fetch_array($result);
the code above generates a warning:
Warning: odbc_fetch_array() [function.odbc-fetch-array]: SQL error: [Microsoft][SQL Server Native Client 10.0][SQL Server]the column name '' is ambiguous., SQL state 37000 in SQLGetData
The error would be okay if i had selected field1 without specifying from which table, but even when it is explicit it doesn't work. the only "workaround" i've found so far is to remove the field from the selected fields or changinf the field name from the second table
I tried with: both SQL Server Native Client 10.0 and SQL Server Native Client 11.0 SQL server 2008 Php 5.3.2 over Windows
PD: If someone has any aideas without using sqlsrv or mssqL extension it will be great
Upvotes: 0
Views: 1076
Reputation: 64
Finally i found out the issue was caused due to a column with "text" type on SQL server database, odbc_fetch_array fails when the query has one or more joins to tables that have the same column name and in the selected fields you include a text type column.
Upvotes: 2
Reputation: 77876
Is this a typo while writing the post cause this looks weird table1.field1=table2=field1
. It should be table1.field1 = table2.field1
SELECT table1.field1, table1.field2 from table1 JOIN table2
ON table1.field1=table2=field1
<--Here
Upvotes: 0