Question

I'm new. Most of the times I find solutions here just by reading but now I have to ask you directly because of the weirdness of my subject.

I have an application in php and I used to use the mssql libraries to connect to MS Server 2008 but now I migrated part of my code to connect through ADODB http://adodb.sourceforge.net/

I have a store procedure which I use to validate/insert/update/delete rows depending of parameters I send, so at the very bottom I have a line of code like this

 Select @Result Result

This variable just tell me everytime if the proccess went correctly or if I'm missing something so the row doesn't get inserted/deleted/updated.

Here the code of my store procedure

        create procedure sp_MyTable @id int, @name varchar(100), @type varchar(10)
        as

        declare @Results varchar(100)
         set @Result=@type
         --validations!
        if exists(select * from MyTable where name=@name)begin

        set @Result='No insert:('
        end

         if @Result='insert'           
          insert into MyTable (name)values(@name)


           select @Result Result

Here and example of code to create my connection in php

        $pQry='exec sp_MyTable @id="0",@name="Hello",@type="insert"';


            require ("php/ExtClases/adodb5/adodb.inc.php");

             $db = ADONewConnection('mssqlnative');
             $db->debug = false;

             $db->Connect($datCon['sServer'], $datCon['UID'], $datCon['PWD'], $datCon['Database']);

            $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

            $rs = $db->Execute($pQry);          

            $rows=$rs->GetRows();

So, it is supposed to return and Array like this

 print_r($rows);
 //Array ([0]=>Array( [Result] => insert )) ) 

But It prints just nothing.

Using Management Studio I already ran this procedure directly on my own computer and at the server online and both cases retrieved data, so the store procedure is fine.

If I remove the insert statement:

        create procedure sp_MyTable @id int, @name varchar(100), @type varchar(10)
        as

        declare @Results varchar(100)
         set @Result=@type
         --validations!
        if exists(select * from MyTable where name=@name)begin

        set @Result='No insert:('
        end

        /*
           No insert!
         */


           select @Result Result

It works!.

          print_r($rows);
          //Array ([0]=>Array( [Result] => insert)) ) 

_UPDATE: even if I print something (print 'something') in the store procedure ADODB ignores the select statement, so the select must be totally alone. I think I will consider searching for another way. _.

PD: Sorry for my bad english.

Regards.

Was it helpful?

Solution

Well, it took a few hours of my appreciated life but at last now I know what is going on.

MSSQL driver use to ignore all but the last statement (as I remember...). So, if you have something like this

    $query="select * from table1; select some, data from table2; select yadda yadda from table3";

    $conn=mysql_connect($sServer,$UID,$PWD);
    $baseDatos=mysql_select_db($Database,$conn);
    $res=mysql_query($query);

        While($row=mssql_fetch_array($res)){
            print($row);
        }

You will have printed the results only of table3. ( ACTUALLY Here it is written that you can have multiple recorset and navigate through them with mssql http://www.php.net/manual/es/function.mssql-next-result.php but at the moment I don't know why it takes just the last one as the first one. Maybe it doesn't recognize prints or inserts as recorsets at all)

ADODB library actually uses SQLSRV to handle connections and queries to any sql server (mssqlnative). SQLSRV is the official driver released for Microsoft so is slightly different. You need to use the function sqlsrv_next_result or it will be return just the first result whatever it is (print,insert,select,etc)

here the link http://www.php.net/manual/en/function.sqlsrv-next-result.php and another guy who had the same problem SQLSRV and multiple selects in Stored Procedure

what I did is not pretty but with patience I think I can fix it in the future, for now I just need the last recorset (table3) and not the first, so ...

    /*here my temporary fix*/
        do {
        $result = array();
           while ($row = sqlsrv_fetch_array($this->_queryID)) {
               $result[] = $row;
           }
        } while (!is_null(sqlsrv_next_result($this->_queryID)));

        return $result;
              /**/

I overwrite the variable $result with every step into the results :). Funny code.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top