php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40687 ODBC+PHP+MSSQL generate error while executing multi query in one transaction
Submitted: 2007-03-02 00:43 UTC Modified: 2007-03-05 09:50 UTC
From: scottsdev at gmail dot com Assigned:
Status: Not a bug Package: ODBC related
PHP Version: 5.2.1 OS: Windows Server
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: scottsdev at gmail dot com
New email:
PHP Version: OS:

 

 [2007-03-02 00:43 UTC] scottsdev at gmail dot com
Description:
------------
Hello,

I used DSN to established connection with MSSQL using following Cursor.  
Code is >>> 

$conn = odbc_pconnect($dsn,$username,$password, SQL_CUR_USE_ODBC) or die("ERROR OCCUR WHILE CONNECTING TO THE SERVER");

I am getting following error when i tried to execute query like ...
$sql = "insert into <TBL_NAME> (col_1, col_2, col_3) values (val_1, val_2, val_3); select SCOPE_IDENTITY() as LastInsertedID";
$results = odbc_exec($conn, $sql) or die("<br><pre>Query fail: $query</pre>");

//////////////////////////////
if(!$results) 
{
	$this->error("<H2>No results!</H2>\n");
	return false;
}else {
	$data = array();
	while ( $row = odbc_fetch_array($results))
	{
		$data = $row;
	}
	odbc_free_result($results);
	$last_Id = $data['LastInsertedID'];
}
			
==================================================

So errors occurs
on this line: 	while ( $row = odbc_fetch_array($results))

Error is : 
Warning: odbc_fetch_array() [function.odbc-fetch-array]: No tuples available at this result index in D:\inetpub\wwwroot\php\xyz\lib\xxxx.php on line 11111

So I really don't understand how to remove this error, and what to do so that such two query can be run at once and return the result.

J. Scott
[scottsdev at gmail dot com]


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-02 18:19 UTC] fmk@php.net
Your query returns multiple results. The first is for the insert statement and the second for the select. Take a look at http://us3.php.net/odbc_next_result to see how to move to the second result.
 [2007-03-02 21:10 UTC] scottsdev at gmail dot com
No Sir,

Still it don't work.  I put the if condition which check whether Next Result is found or not, and it come inside that clause and show error in fetch_array line.

==================

$query = <<<END_SQL
$query;
select SCOPE_IDENTITY();
END_SQL;

$results = odbc_exec($conn, $query) or die("<br><pre>Query fail: $query</pre>");

//////////////////////////////
if(!$results) 
{
	$this->error("<H2>No results!</H2>\n");
	return false;
}else {
	$data = array();
	if (odbc_next_result){
		while ( $row = odbc_fetch_array($results))
		{

=========================================

It still showing same error at 

==>		while ( $row = odbc_fetch_array($results))

Error: 
Warning: odbc_fetch_array() [function.odbc-fetch-array]: No tuples available at this result index in D:\inetpub\wwwroot\php\xyz\aaa.php on line 1111111
 [2007-03-02 21:58 UTC] fmk@php.net
There is no bug here.

odbc_next_result() sjould be called with a odbc result resource as the parameter. That's the only way it can advnance the internal result pointer to the next result.

You code should look like this:

if (odbc_next_result($results)){
    while ($row = odbc_fetch_array($results)) {
        // Do your stuff
    }
}

or

// Move to the last result
while (odbc_next_result($results));
while ($row = odbc_fetch_array($results)) {
    // Do your stuff
}


 [2007-03-03 00:15 UTC] scottsdev at gmail dot com
Hey Gr8,

Thanks Sir.  I really did silly mistake by not providing argument in odbc_next_result.  I passed resource id.  And it start to work.

Thanks for your Gr8 Help.

J. Scott
(Project Manager)
 [2015-01-28 13:10 UTC] chris dot gralike at amis dot nl
I found that counting fields that contain a logical 'NULL' 

example:
SELECT COUNT([db].[table].[field_with_nulls]) as counted
FROM [db].[table]
WHERE {CONDITION}

will sometimes trigger a No tuples at resultset warning.

adding: 
AND [db].[table].[column_being_counted] is not null

might solve the problem.

My setup
FreeDTS > unixODBC > PHP5.x

Rgrds,
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 15:01:28 2024 UTC