{"id":1736,"date":"2017-12-18T13:46:26","date_gmt":"2017-12-18T04:46:26","guid":{"rendered":"http:\/\/kuuur.net\/?p=1736"},"modified":"2017-12-21T16:28:28","modified_gmt":"2017-12-21T07:28:28","slug":"php-mysql_insert","status":"publish","type":"post","link":"https:\/\/kuuur.net\/?p=1736","title":{"rendered":"PHP\u3067MySQL\u3092\u64cd\u4f5c\u3059\u308b\uff1a(2) \u30ec\u30b3\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b"},"content":{"rendered":"<p>PHP\u304b\u3089MySQL\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30ec\u30b3\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<h2>\u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020<\/h2>\n<p>\u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020\u3092\u4ee5\u4e0b\u3068\u3057\u307e\u3059\u3002<br \/>\n\u6570\u5024\u578b\u3001\u30c6\u30ad\u30b9\u30c8\u578b\u3001\u65e5\u4ed8\u578b\u3001Yes\/No\u578b\u306e4\u7a2e\u985e\u3092\u4f8b\u793a\u3057\u307e\u3059\u3002<br \/>\n\u203bbit\u578b\u3092\u3053\u3053\u3067\u306fYes\/No\u578b\u3068\u3057\u3066\u6271\u3044\u307e\u3059\uff08\u5b9f\u969b\u306f\u6570\u5024\u578b\uff09\u3002<\/p>\n<table class=\"t2\">\n<tbody>\n<tr>\n<th>\u30d5\u30a3\u30fc\u30eb\u30c9\u540d<\/th>\n<th>\u578b<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>id<\/td>\n<td>int(11)<\/td>\n<td>primal_key\uff08\u4e3b\u30ad\u30fc\uff09\u3001auto_increment\uff08\u81ea\u52d5\u63a1\u756a\uff09\u3002<\/td>\n<\/tr>\n<tr>\n<td>code<\/td>\n<td>int(11)<\/td>\n<td>\u30b3\u30fc\u30c9\u756a\u53f7\u3002<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>varchar(255)<\/td>\n<td>\u540d\u524d\u3002<\/td>\n<\/tr>\n<tr>\n<td>date<\/td>\n<td>date<\/td>\n<td>\u65e5\u4ed8\u578b\u3002<\/td>\n<\/tr>\n<tr>\n<td>flag<\/td>\n<td>bit(1)<\/td>\n<td>Yes\/No\u578b\u3067\u51e6\u7406\u3057\u305f\u3044\u30d5\u30a3\u30fc\u30eb\u30c9\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>PHP\u304b\u3089\u30ec\u30b3\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b<\/h2>\n<h3>PHP\u30b3\u30fc\u30c9<\/h3>\n<p class=\"source\">&lt;?php<br \/>\n\/\/ \u5b9a\u6570<br \/>\ndefine(&#8216;HOST&#8217;,&#8217;localhost&#8217;); \/\/ \u30db\u30b9\u30c8\u540d<br \/>\ndefine(&#8216;DBNAME&#8217;,&#8217;database&#8217;); \/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d<br \/>\ndefine(&#8216;ID&#8217;,&#8217;database_id&#8217;); \/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ed\u30b0\u30a4\u30f3ID<br \/>\ndefine(&#8216;PASSWORD&#8217;,&#8217;database_password&#8217;); \/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ed\u30b0\u30a4\u30f3\u30d1\u30b9\u30ef\u30fc\u30c9<br \/>\ndefine(&#8216;TBNAME&#8217;,&#8217;table&#8217;); \/\/ \u30c6\u30fc\u30d6\u30eb\u540d<br \/>\n\/\/ MySQL\u306b\u63a5\u7d9a<br \/>\ntry{<br \/>\n$sql = new PDO( &#8216;mysql:host=&#8217;.HOST.&#8217;; dbname=&#8217;.DBNAME.&#8217;; charset=utf8&#8242;, ID, PASSWORD);<br \/>\nprint &#8216; \u63a5\u7d9a\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002&#8217;;<br \/>\n} catch ( PDOException $e) {<br \/>\nprint &#8220;\u63a5\u7d9a\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002:{$e-&gt;getMessage()}&#8221;;<br \/>\n}<br \/>\n\/\/ \u30ec\u30b3\u30fc\u30c9\u306e\u633f\u5165<br \/>\n$code = 1; \/\/ \u633f\u5165\u3059\u308b\u30b3\u30fc\u30c9\u756a\u53f7<br \/>\n$name = &#8220;\u540d\u524d&#8221;; \/\/ \u633f\u5165\u3059\u308b\u540d\u524d<br \/>\n$date = &#8220;2018-1-1&#8221;; \/\/ \u633f\u5165\u3059\u308b\u65e5\u4ed8<br \/>\n$flag = 1; \/\/ \u633f\u5165\u3059\u308bYes\/No\u578b\u30c7\u30fc\u30bf<br \/>\n$ir -&gt; $sql -&gt; prepare(&#8216;INSERT INTO &#8216;.TBNAME.&#8217; ( code, name, date, flag) VALUES (:code, :name, :date, :flag)&#8217; );<br \/>\n$ir -&gt; bindParam(&#8216;:code&#8217;, $code, PDO::PARAM_INT);<br \/>\n$ir -&gt; bindParam(&#8216;:name&#8217;, $name, PDO::PARAM_STR);<br \/>\n$ir -&gt; bindParam(&#8216;:date&#8217;, $date, PDO::PARAM_STR);<br \/>\n$ir -&gt; bindParam(&#8216;:flag&#8217;, $flag, PDO::PARAM_INT);<br \/>\n$ir -&gt; execute();<br \/>\nprint &#8216;\u767b\u9332\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002&#8217;;<br \/>\n$sql = null;<br \/>\n?&gt;<\/p>\n<p>\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u30ec\u30b3\u30fc\u30c9\u304c1\u4ef6\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u304b\u3089MySQL\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30ec\u30b3\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002 \u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020 \u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020\u3092\u4ee5\u4e0b\u3068\u3057\u307e\u3059\u3002 \u6570\u5024\u578b\u3001\u30c6\u30ad\u30b9\u30c8\u578b\u3001\u65e5\u4ed8\u578b\u3001Yes\/No\u578b\u306e4\u7a2e\u985e\u3092\u4f8b\u793a\u3057\u307e\u3059\u3002 \u203bbit\u578b\u3092\u3053\u3053\u3067\u306f&#8230;<span class=\"continue\"><a href=\"https:\/\/kuuur.net\/?p=1736\">\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-1736","post","type-post","status-publish","format-standard","hentry","category-php"],"views":1909,"_links":{"self":[{"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts\/1736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1736"}],"version-history":[{"count":7,"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts\/1736\/revisions"}],"predecessor-version":[{"id":1772,"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts\/1736\/revisions\/1772"}],"wp:attachment":[{"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}