php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13931 unexpected SQL error using SQL_CUR_USE_ODBC in odbc_pconnect()
Submitted: 2001-11-05 06:57 UTC Modified: 2002-06-25 03:02 UTC
From: g dot sollis at syntegra dot nl Assigned:
Status: Closed Package: ODBC related
PHP Version: 4.1.0 OS: Windows NT4 SP6a
Private report: No CVE-ID: None
 [2001-11-05 06:57 UTC] g dot sollis at syntegra dot nl
I am using the pre-compiled version of PHP 4.06 on Win32. The web server is IIS-4 on WinNT with Option Pack and the database is MS SQL-Server 6.5. Connection is made via ODBC using a system DSN. I am using the latest MDAC(2.6) and JET(4) drivers on this DSN.

What i want to do is the following:
* the user creates a query (by form) that results in 1 or more projects being retrieved.
* if more than 1 projects are found i want to display a shortlist of projects. The user should be able to sort the result on all displayed columns.
When a project is selected, the form submits and retrieves the details for that project. The project is retrieved using the primary key in the WHERE clause and there is no ORDER BY clause:
$qry="SELECT projects.proj_num, projects.status, clients.name FROM 
projects INNER JOIN clients ON projects.clientID=clients.clientID
WHERE projects.proj_num=12345";
* if one project is found, the form should display the project details. 

projects.proj_num and clients.clientID are the table's primary key.

The code I am running is basically this:

$qry="SELECT projects.proj_num, projects.status, clients.name FROM 
projects INNER JOIN clients ON projects.clientID=clients.clientID
WHERE projects.proj_status='Open'";

$conID=ODBC_PCONNECT(DSN,USER,PASSWORD);
$res  =ODBC_DO($conID,$qry);
$rowCnt=0;
while(odbc_fetch_row($res)) {
  $rowCnt=$rowCnt+1;
}
odbc_fetch_row($res,1)
if($rowCnt==1) {
  //display the detailed info function here
  //because we only have found one project.
} else {
  do {
    //this section displays a list of found projects
    //display some fields using odbc_result() here.
  } while(odbc_fetch_row($res);
}

situation 1:
------------
Using the default cursor by specifying 
ODBC_PCONNECT(DSN,USER,PASSWORD) and then performing the query by i get a result set which i can display.
Then a user selects a column to sort and the query gets the following addition (example):

" ORDER BY clients.name ASC"

The form is submitted using the POST method. Trying to sort on a different column than the primary key returns only the last row, if more than one project is found. The query has only changed in that an ORDER BY clause has been added.
I understand this behaviour is due to the driver not supporting row numbers in ODBC_RESULT(), even though it does work from MS-Access or the MS-Query.

In short, i can get one or more projects, but cannot sort on any column except the primary key.

situation 2:
------------
In this situation i have changed the connection by specifying it to use an ODBC cursor:

$conID=ODBC_PCONNECT(DSN,USER,PASSWORD,SQL_CUR_USE_ODBC);

Now i can sort through the list to my heart's content, but when a project is selected it does not work. 
I get the following error from SQL in this situation:
"AMBIGUOUS COLUMN NAME clientID".

I don't understand how this can happen as i have specified which fields it should retrieve from each table. Also, i do not get this error using any other software.

I have made a workaround by querying twice using the default CURSOR-type from situation 1 and storing the results in seperate variables. I don't really want to do this due to wasted resources, so please get it fixed, or re-educate me ;)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-05 11:19 UTC] g dot sollis at syntegra dot nl
oops...missed a ')' in my code here at the end of the 'do' loop. I have got this right in my original code ;)
 [2001-11-06 03:26 UTC] g dot sollis at syntegra dot nl
Also, i have seen a similar error if i reference a field which has a relationship with a primary key in another table -  something like projects.userid, which is in the users table - but don't get any fields from that related table.

consider this:
--------------
SELECT projects.proj_num, projects.status, clients.name, projects.user_id 
FROM projects 
INNER JOIN clients ON projects.clientID=clients.clientID
WHERE projects.proj_status='Open';

The ODBC error is 'AMBIGUOUS COLUMN NAME user_id'

This must defenately be a bug, because user_id is only referenced once in the query, with source table 'projects'.

I must add that there is also a field 'user_id' in the 'clients' table, but i am not querying that field. 
Also, there is no relationship between the 'clients.user_id' and the 'projects.user_id'. They are related to the users table, so i can set the 'owner' of a client or project.
 [2002-04-16 14:05 UTC] kalowsky@php.net
The version of PHP that this bug was reported in is too old. Please
try to reproduce this bug in the latest version of PHP (available
from http://www.php.net/downloads.php

If you are still able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".


 [2002-04-17 03:27 UTC] g dot sollis at syntegra dot nl
I will try and do this the following week with the latest version. This is because i am currently on another project outside the office. I will update then.
 [2002-04-23 11:17 UTC] g dot sollis at syntegra dot nl
Have tried with PHP 4.20, now gives different problems. Please also see bug-id 16756 for that description. I have tried 4.1.0 and could reproduce the problem on that.
 [2002-04-23 11:18 UTC] g dot sollis at syntegra dot nl
see above...
 [2002-05-21 23:08 UTC] kalowsky@php.net
Do please try 4.2.1, I made a big mistake in 4.2.0 that wasn't tested/caught that caused queries to not really work at all.  Sorry for the run around.
 [2002-06-22 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2002-06-25 03:02 UTC] g dot sollis at syntegra dot nl
Structure of the program is being changed so this issue is no longer important for me. Please close this call, if not already done so.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 03:01:35 2024 UTC