pg_escape_string

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_escape_string 转义字符串以供查询

说明

pg_escape_string(PgSql\Connection $connection = ?, string $data): string

pg_escape_string() 转义用于查询数据库的字符串。它返回不带引号的 PostgreSQL 格式的转义字符串。pg_escape_literal() 是为 PostgreSQL 转义 SQL 参数的首选方法。addslashes() 不得与 PostgreSQL 一起使用。如果列的类型是 bytea,则必须改用 pg_escape_bytea()pg_escape_identifier() 必须用于转义标识符(例如表名、字段名)

注意:

本函数需要 PostgreSQL 7.2 及其更高版本。

参数

connection

An PgSql\Connection instance. When connection is unspecified, the default connection is used. The default connection is the last connection made by pg_connect() or pg_pconnect().

警告

As of PHP 8.1.0, using the default connection is deprecated.

data

包含要转义的 string

返回值

包含转义数据的 string

更新日志

版本 说明
8.1.0 现在 connection 参数接受 PgSql\Connection 实例,之前接受 资源(resource)

范例

示例 #1 pg_escape_string() 示例

<?php
// 连接到数据库
$dbconn = pg_connect('dbname=foo');

// 读入文本文件(包含撇号和反斜线)
$data = file_get_contents('letter.txt');

// 转义文本数据
$escaped = pg_escape_string($data);

// 将其插入数据库
pg_query("INSERT INTO correspondence (name, data) VALUES ('My letter', '{$escaped}')");
?>

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top