title: Stored procedure only partially executes from PHP
body: <p>I have a stored procedure running in MS SQL 2008 R2 which runs fine in SQL Management Studio.  However, if I run it from PHP it seems to stop execution before finishing.  There is no error returned in SQL and no error returned in the PHP error log.  The procedure simply writes to an error log a number of times using a second stored procedure.  I have cut the code down to a simple loop to demonstrate reproducing the problem.  This is the main stored procedure.</p>
  
  <pre class="lang-sql prettyprint-override"><code>ALTER PROCEDURE [dbo].[usp_failure_test]
  
  AS
  DECLARE
  @counter int,
  @message nvarchar(100)
  
  SET @counter = 0;
  WHILE @counter &lt; 100
    BEGIN
      SET @message = 'COUNT: ' + CAST(@counter AS nvarchar);
      EXEC usp_log 0, 'TEST', @message;
      SET @counter += 1;
    END
  EXEC usp_log 0, 'TEST', 'Finished';
  </code></pre>
  
  <p>This calls the procedure usp_log</p>
  
  <pre class="lang-sql prettyprint-override"><code>ALTER PROCEDURE [dbo].[usp_log]
  @engine_id int,
  @type nvarchar(15),
  @message text
  AS
  DECLARE
  @last int,
  @log_size int
  
  INSERT INTO [dbo].[log] (engine_id, timestamp, type, message) VALUES (@engine_id, getdate(), @type, @message);
  SET @log_size = CONVERT(int, (dbo.get_setting('log_size')))
  SET @last = (SELECT TOP 1 id FROM log ORDER BY id DESC)
  DELETE FROM log WHERE id &lt; (@last - @log_size)
  return
  </code></pre>
  
  <p>If I execute usp_failure test from SSMS it writes all of the expected log entries and then stops successfully.  However, if I execute it from PHP (with the following code), it only reaches 52 and then stops without any clue as to why.</p>
  
  <pre><code>&lt;?php
  $servername = "localhost\sqlexpress";
  $connectionInfo = array("UID" =&gt; "xxx", "PWD" =&gt; "xxx", "Database"=&gt;"xxx", "ReturnDatesAsStrings"=&gt;true, "CharacterSet" =&gt;"UTF-8");
  $conn = @sqlsrv_connect($servername, $connectionInfo);
  $query = "usp_failure_test";
  $params = array();
  $result = sqlsrv_query($conn, $query, $params);
  ?&gt;
  </code></pre>
  
  <p>I have tried adding in extra lines of code and this makes it fail after even fewer iterations.  I have ensured that PHP has plenty of memory to work with and I've also checked the PHP and SQL logs to no avail.  We've also used SQL profiler which did not show up any issues either.</p>