{"id":96424,"date":"2024-10-20T09:18:19","date_gmt":"2024-10-20T01:18:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96424\/"},"modified":"2024-10-20T09:18:19","modified_gmt":"2024-10-20T01:18:19","slug":"php-pdo%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%98%ef%bc%9a%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-php-pdo%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96424\/","title":{"rendered":"PHP PDO\u5b78\u7fd2\u7b46\u8a18\uff1a\u7372\u53d6\u6578\u64da\u5eab\u6578\u64da (php pdo\u7372\u53d6\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"php-pdo%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%98%ef%bc%9a%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-VyxIMLoGIz\">PHP PDO\u5b78\u7fd2\u7b46\u8a18\uff1a\u7372\u53d6\u6578\u64da\u5eab\u6578\u64da<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cPHP\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u4f3a\u670d\u5668\u7aef\u8a9e\u8a00\uff0c\u800cPDO\uff08PHP Data Objects\uff09\u5247\u662fPHP\u4e2d\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u5eab\u8a2a\u554f\u5c64\u3002\u4f7f\u7528PDO\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u4ee5\u4e00\u7a2e\u5b89\u5168\u4e14\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP PDO\u4f86\u7372\u53d6\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u6280\u5de7\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afpdo%ef%bc%9f-VyxIMLoGIz\">\u4ec0\u9ebc\u662fPDO\uff1f<\/h2>\n<p>PDO\u662f\u4e00\u500b\u6578\u64da\u5eab\u62bd\u8c61\u5c64\uff0c\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528\u7d71\u4e00\u7684\u63a5\u53e3\u4f86\u8a2a\u554f\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\u5eab\u3002\u9019\u610f\u5473\u8457\u7121\u8ad6\u662fMySQL\u3001PostgreSQL\u9084\u662fSQLite\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u4ee3\u78bc\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002PDO\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7cfb\u7d71<\/li>\n<li>\u63d0\u4f9b\u9810\u8655\u7406\u8a9e\u53e5\u4ee5\u9632\u6b62SQL\u6ce8\u5165<\/li>\n<li>\u652f\u6301\u4e8b\u52d9\u8655\u7406<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8pdo%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a-VyxIMLoGIz\">\u5982\u4f55\u4f7f\u7528PDO\u7372\u53d6\u6578\u64da<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86PDO\u64f4\u5c55\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u901a\u904e\u4ee5\u4e0b\u6b65\u9a5f\u4f86\u7372\u53d6\u6578\u64da\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-VyxIMLoGIz\">1. \u9023\u63a5\u5230\u6578\u64da\u5eab<\/h3>\n<pre><code>\ntry {\n    $dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8';\n    $username = 'root';\n    $password = 'password';\n    $options = [\n        PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION,\n        PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,\n    ];\n\n    $pdo = new PDO($dsn, $username, $password, $options);\n} catch (PDOException $e) {\n    echo '\u9023\u63a5\u5931\u6557: ' . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86DSN\uff08\u6578\u64da\u6e90\u540d\u7a31\uff09\u4f86\u6307\u5b9a\u6578\u64da\u5eab\u7684\u985e\u578b\u3001\u4e3b\u6a5f\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500bPDO\u5be6\u4f8b\uff0c\u4e26\u8a2d\u7f6e\u4e86\u4e00\u4e9b\u9078\u9805\u4f86\u8655\u7406\u932f\u8aa4\u548c\u9ed8\u8a8d\u7684\u53d6\u56de\u6a21\u5f0f\u3002<\/p>\n<h3 id=\"2-%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a-VyxIMLoGIz\">2. \u7372\u53d6\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8a62\u4f86\u7372\u53d6\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\n$sql = 'SELECT * FROM users WHERE age &gt; :age';\n$stmt = $pdo-&gt;prepare($sql);\n$stmt-&gt;execute(['age' =&gt; 18]);\n\n$results = $stmt-&gt;fetchAll();\n\nforeach ($results as $row) {\n    echo '\u7528\u6236\u540d: ' . $row['username'] . '<br>';\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u9632\u6b62SQL\u6ce8\u5165\u3002`:age`\u662f\u4e00\u500b\u4f54\u4f4d\u7b26\uff0c\u901a\u904e`execute`\u65b9\u6cd5\u50b3\u905e\u5be6\u969b\u7684\u503c\u3002`fetchAll`\u65b9\u6cd5\u5c07\u8fd4\u56de\u6240\u6709\u7b26\u5408\u689d\u4ef6\u7684\u8a18\u9304\u3002<\/p>\n<h3 id=\"3-%e8%99%95%e7%90%86%e9%8c%af%e8%aa%a4-VyxIMLoGIz\">3. \u8655\u7406\u932f\u8aa4<\/h3>\n<p>\u5728\u4f7f\u7528PDO\u6642\uff0c\u8655\u7406\u932f\u8aa4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u932f\u8aa4\u6a21\u5f0f\u4f86\u6355\u7372\u7570\u5e38\uff1a<\/p>\n<pre><code>\ntry {\n    $stmt-&gt;execute(['age' =&gt; 18]);\n} catch (PDOException $e) {\n    echo '\u67e5\u8a62\u5931\u6557: ' . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u67e5\u8a62\u5931\u6557\u6642\uff0c\u80fd\u5920\u7372\u5f97\u8a73\u7d30\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u4fbf\u65bc\u8abf\u8a66\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VyxIMLoGIz\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528PHP PDO\u4f86\u7372\u53d6\u6578\u64da\u5eab\u6578\u64da\u662f\u4e00\u500b\u9ad8\u6548\u4e14\u5b89\u5168\u7684\u9078\u64c7\u3002\u901a\u904e\u9810\u8655\u7406\u8a9e\u53e5\u548c\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62SQL\u6ce8\u5165\u548c\u8655\u7406\u6f5b\u5728\u7684\u932f\u8aa4\u3002\u7121\u8ad6\u662f\u958b\u767c\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0cPDO\u90fd\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u4f60\u7684PHP\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PHP PDO\u7372\u53d6\u6578\u64da\u5eab\u6578\u64da\uff0c\u638c\u63e1\u57fa\u672c\u64cd\u4f5c\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u63d0\u5347\u4f60\u7684\u6578\u64da\u5eab\u7ba1\u7406\u6280\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96424","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96424"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96424\/revisions"}],"predecessor-version":[{"id":96425,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96424\/revisions\/96425"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}