{"id":1731,"date":"2017-12-18T11:51:48","date_gmt":"2017-12-18T02:51:48","guid":{"rendered":"http:\/\/kuuur.net\/?p=1731"},"modified":"2017-12-18T13:47:00","modified_gmt":"2017-12-18T04:47:00","slug":"php-mysql1","status":"publish","type":"post","link":"https:\/\/kuuur.net\/?p=1731","title":{"rendered":"PHP\u3067MySQL\u3092\u64cd\u4f5c\u3059\u308b\uff1a(1) MySQL\u3078\u63a5\u7d9a\u3059\u308b"},"content":{"rendered":"<p>PHP\u304b\u3089MySQL\u306b\u63a5\u7d9a\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\u3002<\/p>\n<h2>PHP\u304b\u3089MySQL\u306b\u63a5\u7d9a\u3059\u308b<\/h2>\n<p class=\"source\">&lt;?php<br \/>\ntry{<br \/>\n$sql = new PDO (&#8216;mysql:host=localhost;dbname=database; charset=utf8; &#8216;database_id&#8217;, &#8216;database_password&#8217;);<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:{$ej-&gt;getMessage()}&#8221;;<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>PDO\u6587\u306e\u300chost=localhost\u300d\u306elocalhost\u306b\u306f\u30db\u30b9\u30c8\u540d\u3092\u66f8\u304d\u307e\u3059\u3002<br \/>\n\u300cdbname=database\u300d\u306edatabase\u306b\u306fMySQL\u3067\u8a2d\u5b9a\u3057\u305f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3092\u66f8\u304d\u307e\u3059\u3002<br \/>\n\u300ccharset=utf8\u300d\u306f\u3001\u6587\u5b57\u30b3\u30fc\u30c9\u3067\u666e\u901a\u306f\u5b50\u306e\u307e\u307e\u5909\u66f4\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n\u300c&#8217;database_id&#8217;\u300d\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u63a5\u7d9a\u3059\u308b\u969b\u306e\u30ed\u30b0\u30a4\u30f3\u540d\u3092\u66f8\u304d\u307e\u3059\u3002<br \/>\n\u300c&#8217;database_password&#8217;\u300d\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u63a5\u7d9a\u3059\u308b\u969b\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u66f8\u304d\u307e\u3059\u3002<\/p>\n<h2>\u30db\u30b9\u30c8\u540d\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3001ID\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306a\u3069\u3092\u5b9a\u6570\u306b\u3059\u308b<\/h2>\n<p>\u30db\u30b9\u30c8\u540d\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3001\u30ed\u30b0\u30a4\u30f3ID\u3001\u30ed\u30b0\u30a4\u30f3\u30d1\u30b9\u30ef\u30fc\u30c9\u306a\u3069\u306f\u3001\u4e00\u62ec\u3067\u7ba1\u7406\u3057\u305f\u65b9\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u306e\u3067\u3001\u5b9a\u6570\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p class=\"source\">define(&#8216;HOST&#8217; , &#8216;localhost&#8217;);<br \/>\ndefine(&#8216;DBNAME&#8217;, &#8216;database&#8217;);<br \/>\ndefine(&#8216;ID&#8217;, &#8216;database_id&#8217;);<br \/>\ndefine(&#8216;PASSWORD&#8217;, &#8216;database_password&#8217;);<br \/>\n$sql = new PDO (&#8216;mysql:host=&#8217;.HOST.&#8217;; dbname=&#8217;.DBNAME.&#8217;; charset=utf8&#8242;, ID, PASSWORD);<\/p>\n<p>define()\u306f\u3001\u5b9a\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u95a2\u6570\u3067\u3059\u3002<br \/>\n\u6700\u521d\u306e\u5024\u304c\u5b9a\u6570\u540d\u3001\u6b21\u306e\u5024\u304c\u5b9a\u6570\u306e\u5024\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u5b9a\u6570\u540d\u306f\u5927\u6587\u5b57\u3059\u308b\u3001\u3068\u3044\u3046\u6697\u9ed9\u306e\u30eb\u30fc\u30eb\u304c\u4e00\u90e8\u306b\u306f\u3042\u308b\u306e\u3067\u3001\u3053\u3053\u3067\u306f\u305d\u308c\u306b\u5247\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u305f\u3060\u3057\u3001\u5927\u6587\u5b57\u3067\u3082PHP\u306e\u4e88\u7d04\u8a9e\u3068\u30d0\u30c3\u30c6\u30a3\u30f3\u30b0\u3057\u3066\u3057\u307e\u3046\u3068\u6a5f\u80fd\u3057\u306a\u304f\u306a\u308b\u306e\u3067\u3001\u30d0\u30c3\u30c6\u30a3\u30f3\u30b0\u3057\u306a\u3044\u30cd\u30fc\u30df\u30f3\u30b0\u306b\u3059\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\uff08\u5148\u982d\u306bT_\u3092\u3064\u3051\u308b\u306a\u3069\uff09\u3002<\/p>\n<p>\u306a\u304a\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306bPHP\u3067\u306f\u3001\u6587\u5b57\u5217\u306b\u5909\u6570\u306e\u5024\u3092\u3064\u306a\u3052\u308b\u306b\u306f\u3001\u30d4\u30ea\u30aa\u30c9\uff08.\uff09\u3092\u4f7f\u3044\u307e\u3059\uff08&#8217;mysql:host=&#8217;.HOST\uff5e\uff09\u3002<\/p>\n<h2>\u5b9a\u6570\u3092\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u51fa\u3057\u3066\u8aad\u307f\u8fbc\u3080<\/h2>\n<p>\u5b9a\u6570\u3092\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u306b\u307e\u3068\u3081\u3066\u3001\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u5b9a\u6570\u3092\u307e\u3068\u3081\u305f\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u3092\u300cconstant.php\u300d\u3001\u8aad\u307f\u8fbc\u3080\u30d5\u30a1\u30a4\u30eb\u3092\u300cindex.php\u300d\u3068\u3057\u307e\u3059\u3002<\/p>\n<h3>constant.php<\/h3>\n<p class=\"source\">&lt;?php<br \/>\ndefine(&#8216;HOST&#8217; , &#8216;localhost&#8217;);<br \/>\ndefine(&#8216;DBNAME&#8217;, &#8216;database&#8217;);<br \/>\ndefine(&#8216;ID&#8217;, &#8216;database_id&#8217;);<br \/>\ndefine(&#8216;PASSWORD&#8217;, &#8216;database_password&#8217;);<br \/>\n?&gt;<\/p>\n<h3>index.php<\/h3>\n<p class=\"source\">&lt;?php<br \/>\ninclude &#8216;constant.php&#8217;;<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&#8217;;<br \/>\n} catch (PDOException $e) {<br \/>\nprint &#8220;\u63a5\u7d9a\u30a8\u30e9\u30fc:{$ej-&gt;getMessage()}&#8221;;<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3068\u304d\u306f\u3001include\u3092\u4f7f\u3044\u307e\u3059\u3002<br \/>\n\u3053\u306e\u307b\u304b\u3001\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u95a2\u6570\u306b\u306f\u3001require\u3001include_once\u3001require_once\u3001autoload\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u304b\u3089MySQL\u306b\u63a5\u7d9a\u3059\u308b\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\u3002 PHP\u304b\u3089MySQL\u306b\u63a5\u7d9a\u3059\u308b &lt;?php try{ $sql = new PDO (&#8216;mysql:host=local&#8230;<span class=\"continue\"><a href=\"https:\/\/kuuur.net\/?p=1731\">\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-1731","post","type-post","status-publish","format-standard","hentry","category-php"],"views":1896,"_links":{"self":[{"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts\/1731","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=1731"}],"version-history":[{"count":5,"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts\/1731\/revisions"}],"predecessor-version":[{"id":1742,"href":"https:\/\/kuuur.net\/index.php?rest_route=\/wp\/v2\/posts\/1731\/revisions\/1742"}],"wp:attachment":[{"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kuuur.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}