alex
alex

Reputation: 2209

MS SQL Query failed in PHP but not in MS SQL Server Management Studio

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

Upvotes: 3

Views: 1051

Answers (2)

dharanbro
dharanbro

Reputation: 1342

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

    }
}

Upvotes: 0

Andomar
Andomar

Reputation: 238176

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

Upvotes: 2

Related Questions