1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
archived-doc-zh/reference/pgsql/functions/pg-select.xml

189 lines
6.6 KiB
XML
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 18aa2012f6fa1e5b09733147e02911d16e06d4a1 Maintainer: HonestQiao Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.pg-select" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>pg_select</refname>
<refpurpose>
选择记录
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>array</type><type>string</type><type>false</type></type><methodname>pg_select</methodname>
<methodparam><type>PgSql\Connection</type><parameter>connection</parameter></methodparam>
<methodparam><type>string</type><parameter>table_name</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>conditions</parameter><initializer>[]</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer><constant>PGSQL_DML_EXEC</constant></initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>mode</parameter><initializer><constant>PGSQL_ASSOC</constant></initializer></methodparam>
</methodsynopsis>
<para>
<function>pg_select</function> 根据 <literal>conditions</literal> 数组中的 <literal>field=&gt;value</literal> 值来选择记录。成功的查询返回和 <literal>conditions</literal>
指定的条件相匹配的包括记录和字段的数组。
</para>
<para>
如果设置了 <literal>flags</literal><function>pg_convert</function> 会按照指定 flag 作用于 <literal>conditions</literal> 上。
</para>
<para>
如果设置了 <parameter>mode</parameter>,返回值将以数组形式返回,其中使用 <constant>PGSQL_NUM</constant>
表示索引数组,使用 <constant>PGSQL_ASSOC</constant> 表示关联数组(默认),或者同时使用 <constant>PGSQL_BOTH</constant> 表示两者。
</para>
<para>
默认情况下 <function>pg_select</function> 传递原始值。值必须转义或指定 PGSQL_DML_ESCAPE。PGSQL_DML_ESCAPE
引用并转义参数/标识符。因此,表/列名称变得区分大小写。
</para>
<para>
注意转义和预处理查询都不能保护 LIKE 查询、JSON、Array、Regex 等,这些参数要根据上下文来处理。即转义/验证值。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>connection</parameter></term>
<listitem>
&pgsql.parameter.connection;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>table_name</parameter></term>
<listitem>
<para>
要选择记录的表名。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>conditions</parameter></term>
<listitem>
<para>
<type>array</type>,其键是表 <parameter>table_name</parameter> 中的字段名称,其值是检索记录必须满足的条件。
自 PHP 8.4.0 起,当提供空数组时,不会应用任何条件。
以前,当 <parameter>conditions</parameter> 参数为空时,函数失败。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
任意数量的 <constant>PGSQL_CONV_FORCE_NULL</constant><constant>PGSQL_DML_NO_CONV</constant><constant>PGSQL_DML_ESCAPE</constant><constant>PGSQL_DML_EXEC</constant><constant>PGSQL_DML_ASYNC</constant>
<constant>PGSQL_DML_STRING</constant> 的组合。当 <constant>PGSQL_DML_STRING</constant><parameter>flags</parameter>
的一部分,然后将返回查询字符串。当设置了 <constant>PGSQL_DML_NO_CONV</constant><constant>PGSQL_DML_ESCAPE</constant>
时,不会在内部调用 <function>pg_convert</function>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>mode</parameter></term>
<listitem>
<para>
<constant>PGSQL_ASSOC</constant><constant>PGSQL_NUM</constant><constant>PGSQL_BOTH</constant> 中的一个。
如果设置了 <constant>PGSQL_ASSOC</constant>,返回值将是关联 <type>array</type>;如果设置了 <constant>PGSQL_NUM</constant>,返回值将是索引
<type>array</type>;如果设置了 <constant>PGSQL_BOTH</constant>,返回值将是同时包含关联和索引的 <type>array</type>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
成功时,如果通过 <parameter>flags</parameter> 传递 <constant>PGSQL_DML_STRING</constant>,返回 &string;,否则返回 <type>array</type>&return.falseforfailure;
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.4.0</entry>
<entry>
<parameter>conditions</parameter> 现在是可选的。
</entry>
</row>
&pgsql.changelog.connection-object;
<row>
<entry>7.1.0</entry>
<entry>
新增 <parameter>mode</parameter> 参数。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>pg_select</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
$db = pg_connect('dbname=foo');
// 在某种程度上安全,因为所有值都转义了。
// 然而 PostgreSQL 支持 JSON/Array。无论是
// 转义还是预处理都不安全。
$rec = pg_select($db, 'post_log', $_POST, PG_DML_ESCAPE);
if ($rec) {
echo "Records selected\n";
var_dump($rec);
} else {
echo "User must have sent wrong inputs\n";
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>pg_convert</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
-->