title: Running MSSQL Stored Procedures via ODBC (And getting the results back) with PHP
body: <p>My work is in the process of moving all of our PHP web applications off of our Windows production web server to a new Linux server environment specifically for PHP apps. The sticky part is, several of the PHP applications we're running are using MSSQL databases, and therefore all the DB connectivity has to be re-written to use unixODBC/FreeTDS since PHP on Linux doesn't support mssql_connect() and it's related functions.</p>
  
  <p>I've gotten the connectivity down and basic queries execute fine. The problem I'm having is with running stored procedures, and replicating the mssql_init()/mssql_bind()/mssql_execute() functionality via ODBC.</p>
  
  <p>The problem area I'm working on is the following:</p>
  
  <pre><code>$sp = mssql_init('sp_Search', $this-&gt;_link);
  mssql_bind($sp, '@search', $this-&gt;_searchString, SQLVARCHAR);
  $results = mssql_execute($sp);
  </code></pre>
  
  <p>I've read a bunch about the various ODBC functions and MSSQL functions. I've tried using the odbc_prepare()/odbc_execute() functions, to no avail (I always receive HY000 error codes from the SQL server). The closest I've come is this:</p>
  
  <pre><code>$results = odbc_exec($this-&gt;_link, "EXEC sp_Search @search='@this-&gt;_searchString'");
  </code></pre>
  
  <p>Running that query ("EXEC sp_Search @search='blah'") via the MSSQL Management Console works perfectly, but via PHP it seems to work, but if I try to fetch the rows from the result set, I get "No tuples available at this result index" errors. And odbc_next_result() returns false no matter what.</p>
  
  <p>I also tried setting the cursor on the connection to SQL_CUR_USE_ODBC but that didn't help either. Does anybody have any experience doing this? Is it even possible, or is that functionality simply not available without the native MSSQL driver?</p>
  
  <h2><strong>Edit:</strong></h2>
  
  <p>Just to clarify, I have tried using odbc_prepare() and odbc_execute() as follows:</p>
  
  <pre><code>$results = odbc_prepare($this-&gt;_link, "{CALL sp_Search(?)}");
  $params = array($this-&gt;_searchString);
  odbc_execute($results, $params);
  </code></pre>
  
  <p>This results in the following error:</p>
  
  <pre><code>Warning: odbc_execute(): SQL error: Failed to fetch error message, SQL state HY000 in SQLExecute
  </code></pre>