-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathpg-query.xml
executable file
·186 lines (170 loc) · 5.29 KB
/
pg-query.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: c2eca73ef79ebe78cebb34053e41b565af504c4f Maintainer: dallas Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<!-- splitted from ./en/functions/pgsql.xml, last change in rev 1.2 -->
<refentry xml:id="function.pg-query" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>pg_query</refname>
<refpurpose>执行查询</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>PgSql\Result</type><type>false</type></type><methodname>pg_query</methodname>
<methodparam choice="opt"><type>PgSql\Connection</type><parameter>connection</parameter></methodparam>
<methodparam><type>string</type><parameter>query</parameter></methodparam>
</methodsynopsis>
<para>
<function>pg_query</function> 在特定数据库 <parameter>connection</parameter> 上执行
<parameter>query</parameter>。<function>pg_query_params</function> 在大多数情况下应该是首选。
</para>
<para>
如果发生错误并返回 &false;,那么在连接有效时可以使用 <function>pg_last_error</function> 函数检索错误的详细信息。
</para>
<para>
<note>
<simpara>
尽管可以省略 <parameter>connection</parameter>,但不建议这样做,因为可能会导致脚本中的错误难以发现。
</simpara>
</note>
</para>
<note>
<para>
本函数以前的名字为 <function>pg_exec</function>。<function>pg_exec</function>
因为兼容性原因仍可使用,但鼓励用户使用新名称。
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>connection</parameter></term>
<listitem>
&pgsql.parameter.connection-with-unspecified-default;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>query</parameter></term>
<listitem>
<para>
要执行的 SQL 语句。当多个语句传递给函数时,将作为一个事务自动执行,除非查询字符串中包含明确的 BEGIN/COMMIT 命令。但是,不建议在一个函数调用中使用多个事务。
</para>
<warning>
<para>
用户提供的数据作为字符串插入值非常危险,很可能导致 <link linkend="security.database.sql-injection">SQL
注入</link>漏洞。在大多数情况下,应该首选 <function>pg_query_params</function>,将用户提供的值作为参数传递,而不是将它们替换为查询字符串。
</para>
<para>
任何用户提供的数据,都应该<link linkend="function.pg-escape-string">正确转义</link>,然后直接替换为查询字符串。
</para>
</warning>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
成功时为 <classname>PgSql\Result</classname> 实例,&return.falseforfailure;。
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
&pgsql.changelog.return-result-object;
&pgsql.changelog.connection-object;
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>pg_query</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
$conn = pg_pconnect("dbname=publisher");
if (!$conn) {
echo "An error occurred.\n";
exit;
}
$result = pg_query($conn, "SELECT author, email FROM authors");
if (!$result) {
echo "An error occurred.\n";
exit;
}
while ($row = pg_fetch_row($result)) {
echo "Author: $row[0] E-mail: $row[1]";
echo "<br />\n";
}
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>使用多条语句的 <function>pg_query</function></title>
<programlisting role="php">
<![CDATA[
<?php
$conn = pg_pconnect("dbname=publisher");
// 这些语句将作为一个事务执行
$query = "UPDATE authors SET author=UPPER(author) WHERE id=1;";
$query .= "UPDATE authors SET author=LOWER(author) WHERE id=2;";
$query .= "UPDATE authors SET author=NULL WHERE id=3;";
pg_query($conn, $query);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>pg_connect</function></member>
<member><function>pg_pconnect</function></member>
<member><function>pg_fetch_array</function></member>
<member><function>pg_fetch_object</function></member>
<member><function>pg_num_rows</function></member>
<member><function>pg_affected_rows</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->