3

I execute this query with php and odbc driver

$sql="DECLARE @Auftrag int; 
DECLARE @date_now datetime = getdate(); 
EXEC @Auftrag=EHS.dbo.SP_ANZEIGE 
@Tablet=1, 
@Status=0, 
@KuNr='K015538'; 
SELECT 'generatedID'=@Auftrag;";

$res = odbc_exec($db1_link, $sql) or die(odbc_errormsg()); // returns resource(13)
$firstRow = odbc_fetch_array($res); // dies error

If i do odbc_fetch_array the error "No tuples available at this result index" is thrown.

If I run the exact same query in Management Studio everything works fine. It shows me the computed generatedID. What is the difference?

greets Alex

3
  • Show us, how you do run this query in php Commented Nov 4, 2013 at 9:28
  • 2
    Try to prefix the query with: set nocount on. That prevents SQL Server from sending rowcount updates, which UNIX clients sometimes mistake for actual rowsets. Commented Nov 4, 2013 at 9:30
  • WOW ... set nocount on really did it. I don't know exactly why but I'm really happy :) thanks Commented Nov 4, 2013 at 9:34

2 Answers 2

2

Try to prefix the query with:

set nocount on

That prevents SQL Server from sending rowcount updates, which UNIX clients sometimes mistake for actual rowsets

Sign up to request clarification or add additional context in comments.

Comments

0

I had this same problem. In my case I was executing like this

$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);

while ($data = odbc_fetch_array($resultSet)) {
    $sql = "SELECT * FROM table2";
    $resultSet2 = odbc_exec($sqllink, $sql);//failed here
    while ($data2 = odbc_fetch_array($resultSet2)) {

           //something here

    }
}

and I changed like this and it worked

$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);

// Create an array and store the results

$queryResult = array();

while ($data = odbc_fetch_array($resultSet)) {

    // push the required content into the array

    $queryResult[$data['id']]['name'] = $data[name];
}

foreach($queryResult as $row) {
    $sql = "SELECT * FROM table2";
    $resultSet2 = odbc_exec($sqllink, $sql);
    while ($data2 = odbc_fetch_array($resultSet2)) {

        // something here

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.