<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Encodez Blog &#187; MySQL</title>
	<atom:link href="http://encodez.com/blog/category/webdevelopement/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://encodez.com/blog</link>
	<description>Computer language design is just like a stroll in the park. Jurassic Park, that is. — Larry Wall</description>
	<lastBuildDate>Thu, 22 Jul 2010 05:34:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Escaping &#8216;%&#8217; in MySQL LIKE statement when sprintf</title>
		<link>http://encodez.com/blog/2009/11/escaping-in-mysql-like-statement-when-sprintf/</link>
		<comments>http://encodez.com/blog/2009/11/escaping-in-mysql-like-statement-when-sprintf/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 08:29:12 +0000</pubDate>
		<dc:creator>Muneer Shaheed</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://encodez.com/blog/?p=276</guid>
		<description><![CDATA[I wanted to run a SQL query against MySQL database server which contains search string and need to be formated using <a href="http://php.net/manual/en/function.sprintf.php" target="_blank">sprintf</a>. The problem arise when format. It is because of the sign "%" am using to advance my search term. The query was...
]]></description>
			<content:encoded><![CDATA[<p>I wanted to run a SQL query against MySQL database server which contains search string and need to be formated using <a href="http://php.net/manual/en/function.sprintf.php" target="_blank">sprintf</a>. The problem arise when format. It is because of the sign &#8220;%&#8221; am using to advance my search term. The query was&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user WHERE country = '<span style="color: #009933; font-weight: bold;">%s</span>' AND fName LIKE '<span style="color: #009933; font-weight: bold;">%s</span>%' ORDER BY fName&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span><span style="color: #339933;">,</span> <span style="color: #000088;">$searchTerm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here where the error fires. Thanks God, I found the solution for it.<br />
This can be handled simply as follows,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user WHERE country = '<span style="color: #009933; font-weight: bold;">%s</span>' AND fName LIKE '<span style="color: #009933; font-weight: bold;">%s</span>' ORDER BY fName&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;%&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$searchTerm</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>but for a query like below, where it need multiple formating due to it complex and dynamic generation, I managed to prepare like below and worked fine for me.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fieldArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t3</span>.avatar&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.login&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.firstName&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.lastName&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t2</span>.title&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.email&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT DISTINCT &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.id, <span style="color: #006699; font-weight: bold;">$t3</span>.avatar, <span style="color: #006699; font-weight: bold;">$t1</span>.login, <span style="color: #006699; font-weight: bold;">$t1</span>.firstName, <span style="color: #006699; font-weight: bold;">$t1</span>.lastName, <span style="color: #006699; font-weight: bold;">$t2</span>.title AS groups, <span style="color: #006699; font-weight: bold;">$t1</span>.email, <span style="color: #006699; font-weight: bold;">$t1</span>.active &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;FROM <span style="color: #006699; font-weight: bold;">$t1</span>, <span style="color: #006699; font-weight: bold;">$t2</span>, <span style="color: #006699; font-weight: bold;">$t3</span> &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;WHERE <span style="color: #006699; font-weight: bold;">$t1</span>.id = <span style="color: #006699; font-weight: bold;">$t3</span>.userId AND <span style="color: #006699; font-weight: bold;">$t3</span>.userGroup = <span style="color: #006699; font-weight: bold;">$t2</span>.id &quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$searchField</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$searchField</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;AND &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$searchField</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; LIKE '<span style="color: #009933; font-weight: bold;">%s</span>' &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;%&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$searchVal</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$orderBy</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$orderBy</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%%</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;ORDER BY <span style="color: #009933; font-weight: bold;">%s</span> <span style="color: #009933; font-weight: bold;">%s</span> &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$orderBy</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$orderStyle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%%</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;LIMIT <span style="color: #009933; font-weight: bold;">%d</span>, <span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Before generating the next %, need to double the current % where it will become single % after formated.</p>
<p>Note this</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%%</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://encodez.com/blog/2009/11/escaping-in-mysql-like-statement-when-sprintf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
