Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: pg_escape_string - Manual
[go: Go Back, main page]

PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<pg_escape_byteapg_execute" width="11" height="7"/>
view the version of this page
Last updated: Sat, 21 Jan 2006

pg_escape_string

(PHP 4 >= 4.2.0, PHP 5)

pg_escape_string --  テキスト型フィールドに挿入するために、文字列をエスケープする

説明

string pg_escape_string ( string data )

pg_escape_string() は、データベースに挿入するための 文字列をエスケープします。PostgreSQL フォーマットにエスケープされた 文字列を返します。addslashes() の代わりにこの関数を 使用することを推奨します。カラム型が bytea の場合は、代わりに pg_escape_bytea() を使用しなければなりません。

注意: この関数は、PostgreSQL 7.2 以降が必要です。

パラメータ

data

エスケープするテキスト文字列。

返り値

エスケープされたデータを文字列で返します。

例 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 add a note User Contributed Notes
pg_escape_string
dominik dot mueller at access dot unizh dot ch
16-Jan-2006 02:40
in reply to "rich at dicksonlife dot com"

use serialize() / unserialize() instead!
rich at dicksonlife dot com
19-Jul-2005 09:38
Here's some code I knocked up to turn an array of values into a string representation of an array. Note that I also add the external single quotes to make it a full string literal.

  //$t is array to be escaped. $u will be string literal.
  $tv=array();
  foreach($t as $key=>$val){
   $tv[$key]="\"" .
     str_replace("\"",'\\"', str_replace('\\','\\\\',$val)) . "\"
";
  }
  $u= implode(",",$tv) ;
  $u="'{" . pg_escape_string($u) . "}'";

There's probably a better way of doing this. That's why I'm posting this here :)
tsharek at o2 dot pl
02-Mar-2005 08:34
IMO the stripslashes in this case is not very usefull. Because pg_escape_string change ' into '' (double ' - not "). I use in add to database this:
pg_escape_string(stripslashes($_GET['var'])) and is in 100% safe (i hope).

If I use addslashes in this case that well be lost space in database (\''' - this is 3 bytes)

ps. sorry for my english:)
17-Jul-2003 03:30
Here with 'abc'efg'  the middle ' terminates the string, however 'abc\'def' is one big string with a ' character in the middle.

If the user can terminate the string he can then put in the bad sql.  When prompted for Barcode the user could put in  DROP TABLE foo; SELECT '1

$query = sprintf ("SELECT * FROM a.tblcards WHERE barcode='%s'", pg_escape_string($barcode));

So you have to "clean" your variable coming in to prevent that.

<pg_escape_byteapg_execute" width="11" height="7"/>
 Last updated: Sat, 21 Jan 2006
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2006 The PHP Group
All rights reserved.
This mirror generously provided by: PacketBusiness, Inc.
Last updated: Sun Feb 12 14:11:33 2006 JST