首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

pg_escape_string

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

pg_escape_string - 转义查询字符串

描述

代码语言:javascript
复制
string pg_escape_string ([ resource $connection ], string $data )

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

注意:这个函数需要PostgreSQL 7.2或更高版本。

参数

connection

PostgreSQL数据库连接资源。如果connection不存在,则使用默认连接。默认连接是pg_connect()或pg_pconnect()所做的最后一个连接。

data

包含要转义的文本的字符串。

返回值

包含转义数据的字符串。

更新日志

描述

5.2.0

连接已添加

例子

Example #1 pg_escape_string() example

代码语言:javascript
复制
<?php?
??//?Connect?to?the?database
??$dbconn?=?pg_connect('dbname=foo');
??
??//?Read?in?a?text?file?(containing?apostrophes?and?backslashes)
??$data?=?file_get_contents('letter.txt');
??
??//?Escape?the?text?data
??$escaped?=?pg_escape_string($data);
??
??//?Insert?it?into?the?database
??pg_query("INSERT?INTO?correspondence?(name,?data)?VALUES?('My?letter',?'{$escaped}')");
?>

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com