<?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>爱周末 &#187; 参数化SQL查询</title>
	<atom:link href="http://zhoumo123.cn/tag/%e5%8f%82%e6%95%b0%e5%8c%96sql%e6%9f%a5%e8%af%a2/feed" rel="self" type="application/rss+xml" />
	<link>http://zhoumo123.cn</link>
	<description>知识分享，共同进步。zhoumo123.cn</description>
	<lastBuildDate>Thu, 07 Nov 2019 05:53:49 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.1</generator>
	<item>
		<title>PDO参数化查询prepare() php防SQL注入</title>
		<link>http://zhoumo123.cn/pdo/3535.html</link>
		<comments>http://zhoumo123.cn/pdo/3535.html#comments</comments>
		<pubDate>Thu, 28 Jan 2016 07:28:52 +0000</pubDate>
		<dc:creator><![CDATA[zhangc]]></dc:creator>
				<category><![CDATA[pdo]]></category>
		<category><![CDATA[php pdo]]></category>
		<category><![CDATA[sql注入]]></category>
		<category><![CDATA[参数化SQL查询]]></category>

		<guid isPermaLink="false">http://zhoumo123.cn/?p=3535</guid>
		<description><![CDATA[PDO中参数化查询主要用到prepare()方法，然后这个方法会返回一个PDOStatement对象，也就SQL声明（不知道怎么翻译），此时SQL语句只是被编译，但并未执行，调用PDOStatement中方法后会执行SQL语句，如下示例： 在execute()执行前，就可以调用bindValue()或者bindParam()方法替换之前准备的SQL语句中的你指定参数了，在SQL语句中指定参数有两种方式：':name&#8217;和&#8217;?&#8217;，上面代码中的用的是前一种，后一种的方式是： bindValue()有三个参数，第一个指定要替换掉SQL语句中哪一个参数，第二个指定替换 ...  <a href="http://zhoumo123.cn/pdo/3535.html">  阅读全文 </a>]]></description>
				<content:encoded><![CDATA[<p>PDO中参数化查询主要用到prepare()方法，然后这个方法会返回一个PDOStatement对象，也就SQL声明（不知道怎么翻译），此时SQL语句只是被编译，但并未执行，调用PDOStatement中方法后会执行SQL语句，如下示例：</p>
<pre class="brush: php; title: ; notranslate">
$sm = $db-&gt;prepare('SELECT login_oid FROM logined WHERE user_id=:user_id;');
$sm-&gt;bindValue(':user_id', $user_id, PDO::PARAM_INT);
$sm -&gt; execute();
</pre>
<p>在execute()执行前，就可以调用bindValue()或者bindParam()方法替换之前准备的SQL语句中的你指定参数了，在SQL语句中指定参数有两种方式：':name&#8217;和&#8217;?&#8217;，上面代码中的用的是前一种，后一种的方式是：</p>
<pre class="brush: php; title: ; notranslate">
$sm = $db-&gt;prepare('SELECT * FROM fruit WHERE calories &lt; ?;');
$sm-&gt;bindValue(1, $calories, PDO::PARAM_INT);
$sm-&gt;execute();
</pre>
<p>bindValue()有三个参数，第一个指定要替换掉SQL语句中哪一个参数，第二个指定替换后的值，第三个指定值的类型，类型对应如下：<br />
PDO::PARAM_BOOL<br />
布尔类型</p>
<p>PDO::PARAM_NULL<br />
NULL类型</p>
<p>PDO::PARAM_INT<br />
整数类型</p>
<p>PDO::PARAM_STR<br />
字符串类型如 CHAR, VARCHAR, string</p>
<p>PDO::PARAM_LOB<br />
资源类大对象，如文件等</p>
<p>PDO::PARAM_STMT<br />
不知道</p>
<p>PDO::PARAM_INPUT_OUTPUT<br />
这个好像是扩展类型</p>
<p>里面没有提供实数类型，这个很诧异.</p>
<p>再说说execute()这个方法，它本身也可以做参数替换，但是它会把所有值的类型都变成字符串类型，如下</p>
<pre class="brush: php; title: ; notranslate">
$sm = $db-&gt;prepare('SELECT * FROM fruit WHERE calories &lt; ?;');
$sm-&gt;execute(array($calories));
</pre>
<p>多参数替换如下</p>
<pre class="brush: php; title: ; notranslate">
$sm = $db-&gt;prepare('SELECT * FROM fruit WHERE calories &lt; ?, id &lt; ?;');
$sm-&gt;execute(array($calories, $user_id));
</pre>
<p>http://my.oschina.net/cers/blog/41739?fromerr=ezofpZnY</p>
]]></content:encoded>
			<wfw:commentRss>http://zhoumo123.cn/pdo/3535.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>采用参数化查询SQL语句避免SQL注入</title>
		<link>http://zhoumo123.cn/wangluoanquan/3532.html</link>
		<comments>http://zhoumo123.cn/wangluoanquan/3532.html#comments</comments>
		<pubDate>Thu, 28 Jan 2016 06:44:37 +0000</pubDate>
		<dc:creator><![CDATA[zhangc]]></dc:creator>
				<category><![CDATA[网络安全]]></category>
		<category><![CDATA[参数化SQL查询]]></category>

		<guid isPermaLink="false">http://zhoumo123.cn/?p=3532</guid>
		<description><![CDATA[SQL参数化查询可以防止SQL注入，避免SQL注入的方法有两种： 第一种方案： 所有的SQL语句都存放在存储过程中，这样不但可以避免SQL注入，还能提高一些性能，并且存储过程可以由专门的数据库管理员(DBA)编写和集中管理，不过这种做法有时候针对相同的几个表有不同条件的查询，SQL语句可能不同，这样就会编写大量的存储过程， 第二种方案： 参数化SQL语句。例如我们在本篇中创建的表UserInfo中查找所有女性用户，那么通常情况下我们的SQL语句可能是这样： select * from UserInfo where sex=0 在参数化SQL语句中我们将数值以参数化的形式提供，对于上面的查询，我 ...  <a href="http://zhoumo123.cn/wangluoanquan/3532.html">  阅读全文 </a>]]></description>
				<content:encoded><![CDATA[<p>SQL参数化查询可以防止SQL注入，避免SQL注入的方法有两种：</p>
<p><strong>第一种方案：</strong></p>
<p>所有的SQL语句都存放在存储过程中，这样不但可以避免SQL注入，还能提高一些性能，并且存储过程可以由专门的数据库管理员(DBA)编写和集中管理，不过这种做法有时候针对相同的几个表有不同条件的查询，SQL语句可能不同，这样就会编写大量的存储过程，</p>
<p><strong>第二种方案：</strong></p>
<p>参数化SQL语句。例如我们在本篇中创建的表UserInfo中查找所有女性用户，那么通常情况下我们的SQL语句可能是这样：</p>
<p><span style="color: #0000ff;">select * from UserInfo where sex=0</span></p>
<p>在参数化SQL语句中我们将数值以参数化的形式提供，对于上面的查询，我们用参数化SQL语句表示为：</p>
<p><span style="color: #0000ff;">select * from UserInfo where sex=@sex</span></p>
<p>再对代码中对这个SQL语句中的参数进行赋值，假如我们要查找UserInfo表中所有年龄大于30岁的男性用户，这个参数化SQL语句可以这么写：</p>
<p><span style="color: #0000ff;">select * from UserInfo where sex=@sex and age&gt;@age</span></p>
<p>下面是执行这个查询并且将查询结果集以DataTable的方式返回的代码：</p>
<pre class="brush: php; title: ; notranslate">
//实例化Connection对象
SqlConnection connection = new SqlConnection(&quot;server=localhost;database=pubs;uid=sa;pwd=''&quot;);
//实例化Command对象
SqlCommand command = new SqlCommand(&quot;select * from UserInfo where sex=@sex and age&amp;gt;@age&quot;, connection);
//第一种添加查询参数的例子
command.Parameters.AddWithValue(&quot;@sex&quot;, true);
//第二种添加查询参数的例子
SqlParameter parameter = new SqlParameter(&quot;@age&quot;, SqlDbType.Int);//注意UserInfo表里age字段是int类型的
parameter.Value = 30;
command.Parameters.Add(parameter);//添加参数
//实例化DataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();

</pre>
<p>上面的代码是访问SQL Server数据库的代码。如果本文中提到的数据分别在Access、MySQL、Oracle数据库，那么对应的参数化SQL语句及参数分别如下：</p>
<table cellpadding="1" border="1" cellspacing="1" width="98%">
<tbody>
<tr>
<td width="8%">数据库</td>
<td width="29%">Access</td>
<td width="32%">MySQL</td>
<td width="31%">Oracle</td>
</tr>
<tr>
<td> SQL语句</td>
<td>select * from UserInfo <br />
        where sex=? and age&gt;?</td>
<td>select * from UserInfo <br />
        where sex=?sex and age&gt;?age</td>
<td>select * from UserInfo <br />
        where sex=:sex and age&gt;:age</td>
</tr>
<tr>
<td>参数</td>
<td>OleDbParameter</td>
<td>MySqlParameter</td>
<td>OracleParameter</td>
</tr>
<tr>
<td>实例化参数</td>
<td>OleDbParameter p=new OleDbParameter(&ldquo;?&rdquo;, OleDbType. Boolean);</td>
<td>MySqlParameter p=new MySqlParameter(&ldquo;?sex&rdquo;, MySqlDbType.Bit);</td>
<td>OracleParameter p=new OracleParameter(&ldquo;:sex&rdquo;, OracleType.Byte);</td>
</tr>
<tr>
<td>赋值</td>
<td>p.Value=true;</td>
<td>p.Value=1;</td>
<td>p.Value=1;</td>
</tr>
</tbody>
</table>
<p>http://www.cnblogs.com/aito/archive/2010/08/25/1808569.html</p>
]]></content:encoded>
			<wfw:commentRss>http://zhoumo123.cn/wangluoanquan/3532.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
