note 109326 modified in function.mssql-execute by danbrown

From: Date: Mon, 16 Jul 2012 15:46:08 +0000
Subject: note 109326 modified in function.mssql-execute by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189222@lists.php.net to get a copy of this message
want to share with you that I had a hard time with mssql_execute on a platform that was running php using FastCGI. Well, I guess the problem was that mssql_init always returned null. So after investigating this, and plus the fact that I don&#39;t have total control over the platform, I gave up and used mssql_query instead.&#13;&#10;&#13;&#10;Using the function below, it is possible to generate most stored procedure calls, with the added benefits that one doesn&#39;t have to worry to much about datatype mappings, and that the funtion return exception codes and error message. So here goes:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;function _mssql_exec_sp($storedproc,$params) {&#13;&#10;&#9;$varlist = &#34;&#34;;&#13;&#10;&#9;$setlist = &#34;&#34;;&#13;&#10;&#9;$parmlist = &#34;&#34;;&#13;&#10;&#9;$outs = &#34;&#34;;&#13;&#10;&#9;foreach ($params as $key =&#62; $value) {&#13;&#10;&#9;&#9;$quote = strpos($value[&#39;type&#39;],&#39;char&#39;)!==false;&#13;&#10;&#9;&#9;$varlist .= &#34;@$key &#34;.$value[&#39;type&#39;].&#34;,\\n&#34;;&#13;&#10;&#9;&#9;if (isset($value[&#39;value&#39;])) {&#13;&#10;&#9;&#9;&#9;$setlist .= &#34;set @&#34;.$key.&#34;=&#34;.($quote?&#34;&#39;&#34;:&#39;&#39;).$value[&#39;value&#39;].($quote?&#34;&#39;\\n&#34;:&#34;\\n&#34;);&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;$paramlist .= &#34; @&#34;.$key.(isset($value[&#39;out&#39;])?&#39; output,&#39;:&#39;,&#39;);&#13;&#10;&#9;&#9;if (isset($value[&#39;out&#39;])) {&#13;&#10;&#9;&#9;&#9;$outs .= &#34;@$key as $key,&#34;;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;}&#13;&#10;&#9;if (strlen($paramlist)) {&#13;&#10;&#9;&#9;$paramlist = substr($paramlist,0,strlen($paramlist)-1);&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;&#9;$stmt = &#34;begin try\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;declare\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;@ret int&#34;;&#13;&#10;&#9;if (strlen($varlist)) { &#13;&#10;&#9;&#9;$stmt .= &#34;,\\n&#34;;&#13;&#10;&#9;&#9;$stmt .= $varlist;&#13;&#10;&#9;&#9;$stmt = substr($stmt,0,strlen($stmt)-2);&#13;&#10;&#9;}&#13;&#10;&#9;else {&#13;&#10;&#9;&#9;$stmt .= &#34;\\n&#34;;&#13;&#10;&#9;}&#13;&#10;&#9;$outs = &#34;@ret as ret,&#34;.$outs;&#13;&#10;&#9;$outs = substr($outs,0,strlen($outs)-1);&#13;&#10;&#13;&#10;&#9;$stmt .= &#34;\\n&#34;.$setlist;&#13;&#10;&#9;$stmt .= &#34;exec @ret = &#34;.$storedproc.$paramlist.&#34;\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;select &#34;.$outs.&#34;\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;end try\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;begin catch\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;select error_number() as ret,error_message() as errorMsg\\n&#34;;&#13;&#10;&#9;$stmt .= &#34;end catch\\n&#34;;&#13;&#10;&#9;return mssql_query($stmt);&#13;&#10;} &#13;&#10;?&#62;&#13;&#10;&#13;&#10;example call:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10; &#9;$params = array(&#13;&#10;&#9;&#9;&#39;socialid&#39; =&#62; array(&#13;&#10;&#9;&#9;&#9;&#39;type&#39; =&#62; &#39;char(10)&#39;,&#13;&#10;&#9;&#9;&#9;&#39;value&#39; =&#62; $sid&#13;&#10;&#9;&#9;),&#13;&#10;&#9;&#9;&#39;cust_name&#39; =&#62; array(&#13;&#10;&#9;&#9;&#9;&#39;type&#39; =&#62; &#39;varchar(20)&#39;,&#13;&#10;&#9;&#9;&#9;&#39;value&#39; =&#62; &#39;our name&#39;&#13;&#10;&#9;&#9;),&#13;&#10;&#9;&#9;&#39;id&#39; =&#62; array(&#13;&#10;&#9;&#9;&#9;&#39;type&#39; =&#62; &#39;int&#39;,&#13;&#10;&#9;&#9;&#9;&#39;out&#39; =&#62; true&#13;&#10;&#9;&#9;),&#13;&#10;&#13;&#10;&#9;);&#13;&#10;&#9;$result = _mssql_exec_sp(&#39;sp_register_cust&#39;, $params);&#13;&#10;&#9;$row = mssql_fetch_array($result);&#13;&#10;?&#62; --was-- want to share with you that I had a hard time with mssql_execute on a platform that was running php using FastCGI. Well, I guess the problem was that mssql_init always returned null. So after investigating this, and plus the fact that I don't have total control over the platform, I gave up and used mssql_query instead. Using the function below, it is possible to generate most stored procedure calls, with the added benefits that one doesn't have to worry to much about datatype mappings, and that the funtion return exception codes and error message. So here goes: function _mssql_exec_sp($storedproc,$params) { $varlist = ""; $setlist = ""; $parmlist = ""; $outs = ""; foreach ($params as $key => $value) { $quote = strpos($value['type'],'char')!==false; $varlist .= "@$key ".$value['type'].",\n"; if (isset($value['value'])) { $setlist .= "set @".$key."=".($quote?"'":'').$value['value'].($quote?"'\n":"\n"); } $paramlist .= " @".$key.(isset($value['out'])?' output,':','); if (isset($value['out'])) { $outs .= "@$key as $key,"; } } if (strlen($paramlist)) { $paramlist = substr($paramlist,0,strlen($paramlist)-1); } $stmt = "begin try\n"; $stmt .= "declare\n"; $stmt .= "@ret int"; if (strlen($varlist)) { $stmt .= ",\n"; $stmt .= $varlist; $stmt = substr($stmt,0,strlen($stmt)-2); } else { $stmt .= "\n"; } $outs = "@ret as ret,".$outs; $outs = substr($outs,0,strlen($outs)-1); $stmt .= "\n".$setlist; $stmt .= "exec @ret = ".$storedproc.$paramlist."\n"; $stmt .= "select ".$outs."\n"; $stmt .= "end try\n"; $stmt .= "begin catch\n"; $stmt .= "select error_number() as ret,error_message() as errorMsg\n"; $stmt .= "end catch\n"; return mssql_query($stmt); } example call: <?php $params = array( 'socialid' => array( 'type' => 'char(10)', 'value' => $sid ), 'cust_name' => array( 'type' => 'varchar(20)', 'value' => 'our name' ), 'id' => array( 'type' => 'int', 'out' => true ), ); $result = _mssql_exec_sp('sp_register_cust', $params); $row = mssql_fetch_array($result); ?> http://php.net/manual/en/function.mssql-execute.php

« previous php.notes (#189222) next »