<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[使用不定个数的参数构造查询字符串之可变参数个数的函数]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Thu, 16 Jun 2011 05:55:18 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	写CDB类库的时候,就有一个查询是要求写出一个,具有可变参数个数的函数，类似于sprintf，fsql定义了数据格式，v1, v2等变量定义了要替换的值，然后将替换后的字符串作为数据库查询进行执行.<br/>先举一个实现后的例子:<br/>queryf(&quot;select * from glove_user where name = &#039;%s&#039; and site = &#039;%s&#039;&quot;, &#039;glove&#039;, &#039;glovely.info&#039;);<br/>这其实就是一个select语句,其中不同的地方就是第一个参数中的name的值%s用后面的’glove’来替换,site的值%s用后面的’glovely.info’来替换,这些可以替换的参数是不限定个数的.<br/>也就是说这个函数像我们用的sprintf一样,是带有不定个数的参数的.<br/>实例1：<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;?php
function formatString($str) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;if (get_magic_quotes_gpc ()) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str = stripslashes ( $str );
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;if (! is_numeric ( $str )) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//$str = mysqli_real_escape_string ($dbObj,$str);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str = mysql_real_escape_string($str);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;return $str;
&#125;
function sqlParamBind($sqlParam)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if(empty($sqlParam)) return &quot;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;if(is_string($sqlParam)) return $sqlParam;
&nbsp;&nbsp;&nbsp;&nbsp;if(count($sqlParam) &lt;= 1 ) return $sqlParam[0];
&nbsp;&nbsp;&nbsp;&nbsp;for($i=1,$total=count($sqlParam);$i&lt;$total;$i++)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$sqlParam[$i] = formatString($sqlParam[$i]);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;$sqlPart = call_user_func_array(&#039;sprintf&#039;,$sqlParam);
&nbsp;&nbsp;&nbsp;&nbsp;return $sqlPart;
&#125;
$QQ = &quot;372647693&quot;;
$trueName = &quot;Jackxiang&quot;;
$ID = 103;
$sqlBuild = array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;update glove_user set QQ=&#92;&#039;%1$s&#92;&#039;,Version=&#92;&#039;%4$s&#92;&#039;,Name=&#92;&#039;%2$s&#92;&#039;,FTime=now() where ID=&#92;&#039;%3$s&#92;&#039; and FQQ = &#92;&#039;&#92;&#039;&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$QQ,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$trueName,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ID,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;3rd&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
$sql = sqlParamBind($sqlBuild);
echo $sql;
?&gt;
</textarea><br/><br/>两个需要解释：<br/>1.mysqli_real_escape_string于mysql_real_escape_string<br/>一、mysql中：<br/>mysql_escape_string<br/>mysql_real_escape_string<br/>二、mysqli中：<br/>escape_string<br/>real_escape_string<br/>mysqli_real_escape_string<br/>mysqli_escape_string //是mysqli_real_escape_string的别名<br/>You should use mysql_real_escape_string() instead! <br/>This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler as its first argument and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current character set.&nbsp;&nbsp;<br/>2.call_user_func_array<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;?php
function foobar($arg, $arg2) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo __FUNCTION__, &quot; got $arg and $arg2&#92;n&quot;;
&#125;
class foo &#123;
&nbsp;&nbsp;&nbsp;&nbsp;function bar($arg, $arg2) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo __METHOD__, &quot; got $arg and $arg2&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;
// Call the foobar() function with 2 arguments
call_user_func_array(&quot;foobar&quot;, array(&quot;one&quot;, &quot;two&quot;));
// Call the $foo-&gt;bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, &quot;bar&quot;), array(&quot;three&quot;, &quot;four&quot;));
?&gt; 
</textarea><br/>Result:<br/># php call_user_func_array.php<br/>foobar got one and two<br/>foo::bar got three and four<br/> <br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 使用不定个数的参数构造查询字符串之可变参数个数的函数]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>