{"id":47667,"date":"2024-12-02T11:58:42","date_gmt":"2024-12-02T03:58:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47667\/"},"modified":"2024-12-02T11:58:42","modified_gmt":"2024-12-02T03:58:42","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-postgresql-%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47667\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>\u5982\u4f55\u4f7f\u7528 PHP \u8c03\u7528 PostgreSQL \u5916\u90e8\u51fd\u6570\uff1f\u521b\u5efa\u5916\u90e8\u51fd\u6570\uff0c\u4f8b\u5982\u4f7f\u7528 C \u6216 Perl\u3002\u4f7f\u7528 CREATE FUNCTION \u8bed\u53e5\u5c06\u5916\u90e8\u51fd\u6570\u52a0\u8f7d\u5230 PostgreSQL\u3002\u901a\u8fc7 pg_query() \u51fd\u6570\u5728 PHP \u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729929159671c9fc7ee7bf.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528 PostgreSQL \u5916\u90e8\u51fd\u6570<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>PostgreSQL \u63d0\u4f9b\u4e86\u521b\u5efa\u5916\u90e8\u51fd\u6570\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u60a8\u8c03\u7528\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7f16\u5199\u7684\u51fd\u6570\u3002PHP \u662f\u6700\u6d41\u884c\u7684 Web \u5f00\u53d1\u8bed\u8a00\u4e4b\u4e00\uff0c\u56e0\u6b64\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 PHP \u8c03\u7528 PostgreSQL \u5916\u90e8\u51fd\u6570\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5148\u51b3\u6761\u4ef6<\/strong><\/p>\n<p>\u4e3a\u4e86\u9075\u5faa\u672c\u6307\u5357\uff0c\u60a8\u9700\u8981\uff1a<\/p>\n<ul>\n<li>PostgreSQL 9.3 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li>\u7528 C \u8bed\u8a00\u6216 Perl \u8bed\u8a00\u7f16\u8bd1\u7684\u5916\u90e8\u51fd\u6570<\/li>\n<li>PHP 8.0 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<\/ul>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<p><strong>1. \u521b\u5efa\u5916\u90e8\u51fd\u6570<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>add_numbers<\/code> \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u6570\u5b57\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u603b\u548c\u3002\u4ee5\u4e0b\u662f\u7528 C \u8bed\u8a00\u7f16\u5199\u7684\u5916\u90e8\u51fd\u6570\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n\nextern int add_numbers(int a, int b) {\n  return a + b;\n}<\/pre>\n<p>\u6216\u8005\u662f\u7528 Perl \u8bed\u8a00\u7f16\u5199\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>sub add_numbers {\n  my ($a, $b) = @_;\n  return $a + $b;\n}<\/pre>\n<p><strong>2. \u52a0\u8f7d\u5916\u90e8\u51fd\u6570<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u5c06\u5916\u90e8\u51fd\u6570\u52a0\u8f7d\u5230 PostgreSQL \u4e2d\u3002\u4e3a\u6b64\uff0c\u8bf7\u4f7f\u7528 <code>CREATE FUNCTION<\/code> \u8bed\u53e5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>CREATE FUNCTION add_numbers(int, int)\nLANGUAGE C RETURNS int\nAS 'path\/to\/add_numbers'<\/pre>\n<p>\u5bf9\u4e8e Perl \u51fd\u6570\uff0c\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>CREATE FUNCTION add_numbers(int, int)\nLANGUAGE plperl RETURNS int\nAS 'add_numbers'<\/pre>\n<p><strong>3. \u5728 PHP \u4e2d\u4f7f\u7528\u5916\u90e8\u51fd\u6570<\/strong><\/p>\n<p>\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7 PHP \u7684 <code>pg_query()<\/code> \u51fd\u6570\u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1a<\/p>\n<pre>$dbh = pg_connect('host=localhost port=5432 dbname=database user=username password=password');\n\n$result = pg_query($dbh, \"SELECT add_numbers(3, 5)\");\n\nwhile ($row = pg_fetch_row($result)) {\n  echo $row[0] . \"\\n\";\n}\n\npg_close($dbh);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a <code>students<\/code> \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b <code>id<\/code>, <code>name<\/code> \u548c <code>age<\/code> \u5217\u3002\u60a8\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u6765\u8ba1\u7b97\u6bcf\u4e2a\u5b66\u751f\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<p>\u53ef\u4ee5\u7528 Python \u7f16\u5199\u7684\u5916\u90e8\u51fd\u6570\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>import psycopg2\n\ndef average_marks(student_id):\n  conn = psycopg2.connect(\"host=localhost port=5432 dbname=school user=postgres password=my_password\")\n  cursor = conn.cursor()\n  cursor.execute(\"SELECT AVG(marks) FROM marks WHERE student_id = %s\", (student_id,))\n  result = cursor.fetchone()\n  cursor.close()\n  conn.close()\n  return result[0]<\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u8bed\u53e5\u5c06\u51fd\u6570\u52a0\u8f7d\u5230 PostgreSQL \u4e2d\uff1a<\/p>\n<pre>CREATE FUNCTION average_marks(int)\nRETURNS float8\nLANGUAGE plpythonu AS 'average_marks'<\/pre>\n<p>\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5728 PHP \u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/p>\n<pre>$dbh = pg_connect('host=localhost port=5432 dbname=school user=postgres password=my_password');\n\n$result = pg_query($dbh, \"SELECT id, name, average_marks(id) FROM students\");\n\nwhile ($row = pg_fetch_row($result)) {\n  echo $row[0] . \" \" . $row[1] . \" \" . $row[2] . \"\\n\";\n}\n\npg_close($dbh);<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5982\u4f55\u4f7f\u7528 PostgreSQL \u8c03\u7528\u5916\u90e8\u51fd\u6570\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u5982\u4f55\u4f7f\u7528 PHP \u8c03\u7528 PostgreSQL \u5916\u90e8\u51fd\u6570\uff1f\u521b\u5efa\u5916\u90e8\u51fd\u6570\uff0c\u4f8b\u5982\u4f7f\u7528 C \u6216 Perl\u3002\u4f7f\u7528 CREATE FUNCTION \u8bed\u53e5\u5c06\u5916\u90e8\u51fd\u6570\u52a0\u8f7d\u5230 PostgreSQL\u3002\u901a\u8fc7 pg_query() \u51fd\u6570\u5728 PHP \u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u8c03\u7528 PostgreSQL \u5916\u90e8\u51fd\u6570 \u524d\u8a00 PostgreSQL \u63d0\u4f9b\u4e86\u521b\u5efa\u5916\u90e8\u51fd\u6570\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u60a8\u8c03\u7528\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7f16\u5199\u7684\u51fd\u6570\u3002PHP \u662f\u6700\u6d41\u884c\u7684 Web \u5f00\u53d1\u8bed\u8a00\u4e4b\u4e00\uff0c\u56e0\u6b64\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 PHP \u8c03\u7528 PostgreSQL \u5916\u90e8\u51fd\u6570\u81f3\u5173\u91cd\u8981\u3002 \u5148\u51b3\u6761\u4ef6 \u4e3a\u4e86\u9075\u5faa\u672c\u6307\u5357\uff0c\u60a8\u9700\u8981\uff1a PostgreSQL 9.3 \u6216\u66f4\u9ad8\u7248\u672c \u7528 C \u8bed\u8a00\u6216 Perl \u8bed\u8a00\u7f16\u8bd1\u7684\u5916\u90e8\u51fd\u6570 PHP 8.0 \u6216\u66f4\u9ad8\u7248\u672c \u6b65\u9aa4 1. \u521b\u5efa\u5916\u90e8\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47667","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47667"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47667\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}