<?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>HattaraTecLog &#187; 更新</title>
	<atom:link href="http://teclog.hattara.info/?feed=rss2&#038;tag=%E6%9B%B4%E6%96%B0" rel="self" type="application/rss+xml" />
	<link>http://teclog.hattara.info</link>
	<description>LinuxネタとかPHPネタとかの技術ログ</description>
	<lastBuildDate>Tue, 18 Nov 2014 12:15:20 +0000</lastBuildDate>
	<language>ja</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>MySQLの更新系SQLを省略する(autoExecute)</title>
		<link>http://teclog.hattara.info/?p=37</link>
		<comments>http://teclog.hattara.info/?p=37#comments</comments>
		<pubDate>Fri, 10 Oct 2008 09:55:26 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[更新]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=37</guid>
		<description><![CDATA[MySQLでinsertとかUpdateとかするときに、 SQLをかかずにサクサクやる方法があるそうな。 その [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>MySQLでinsertとかUpdateとかするときに、<br />
SQLをかかずにサクサクやる方法があるそうな。<br />
その名も「autoExecute」なり。</p>
<p>Insertしたい場合には、以下のような書き方になるようです。</p>
<blockquote><p>
$test1 = &#8220;test&#8221;;<br />
$table = &#8220;mysql_test_table&#8221;</p>
<p>$Connect = DB::connect($Dsn);<br />
if (DB::iserror($Connect)){<br />
    die($Connect->getMessage());<br />
}</p>
<p>$Value = array(<br />
			&#8216;name&#8217;   => &#8216;hattara&#8217;,<br />
			&#8216;domain&#8217; => &#8216;teclog.hattara.info&#8217;,<br />
			&#8216;col1&#8242;   => $test1,<br />
			&#8216;col2&#8242;   => &#8216;test2&#8242;<br />
		);</p>
<p>$Result = $Connect->autoExecute($table, $Value, DB_AUTOQUERY_INSERT);</p>
<p>if (PEAR::isError($Result)) {<br />
    die($res->getMessage());<br />
}</p>
<p>$Connect->disconnect();
</p></blockquote>
<p>Updateしたい場合には、以下のような書き方になるようです。</p>
<blockquote><p>
$test1 = &#8220;test&#8221;;<br />
$table = &#8220;mysql_test_table&#8221;<br />
$xxxx = &#8220;xxxxx&#8221;</p>
<p>$Connect = DB::connect($Dsn);<br />
if (DB::iserror($Connect)){<br />
    die($Connect->getMessage());<br />
}</p>
<p>$Value = array(<br />
			&#8216;name&#8217;   => &#8216;hattara&#8217;,<br />
			&#8216;domain&#8217; => &#8216;teclog.hattara.info&#8217;,<br />
			&#8216;col1&#8242;   => $test1,<br />
			&#8216;col2&#8242;   => &#8216;test2&#8242;<br />
		);</p>
<p>$Where = &#8220;col1 = &#8216;abcde&#8217; AND col2 = &#8216;&#8221; . $xxxx . &#8220;&#8216;&#8221;;</p>
<p>$Result = $db->autoExecute($table, $Value, DB_AUTOQUERY_UPDATE, $Where);</p>
<p>if (PEAR::isError($Result)) {<br />
    die($res->getMessage());<br />
}</p>
<p>$Connect->disconnect();
</p></blockquote>
<p>まあ、要するに、</p>
<blockquote><p>
autoExecute(<br />
                  <テーブル名>,<br />
                  <変更カラム名と値の連想配列>,<br />
                  <処理指定>,<br />
                  < updateならwhereがあれば><br />
                );
</p></blockquote>
<p>となりますね。<処理指定>の場所には、</p>
<blockquote><p>
　　insertなら「DB_AUTOQUERY_INSERT」<br />
　　updateなら「DB_AUTOQUERY_UPDATE」
</p></blockquote>
<p>が入るわけです。</p>
<p>ただし、残念でならないのが、updateをする場合に、where句を指定できるのですが、<br />
これがプレースホルダに対応していない。<br />
なので、上記のwhereの部分で、変数部分にはエスケープ処理を入れないといけない。<br />
なんか、あと一味足りない感じですね。</p>
<p>参考元：<br />
<a href="http://sane.justblog.jp/blog/2007/06/autoprepareauto_40a7.html">「實松アウトプット」さん >> autoPrepareとautoExecuteのwhere句</a></p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=37</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
