Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id EFA7B634C39 for ; Mon, 15 Jun 2009 13:11:35 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 86864-03 for ; Mon, 15 Jun 2009 13:11:34 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-gw29.mailanyone.net (smtp-gw29.mailanyone.net [208.70.128.55]) by mail.postgresql.org (Postfix) with ESMTP id E4E98634C38 for ; Mon, 15 Jun 2009 13:11:34 -0300 (ADT) Received: from mailanyone.net by smtp-gw29.mailanyone.net with esmtpa (MailAnyone extSMTP andy@networkmail.eu) id 1MGEmT-00006g-7O; Mon, 15 Jun 2009 11:11:33 -0500 Message-ID: <4A367281.2020900@networkmail.eu> Date: Mon, 15 Jun 2009 17:10:41 +0100 From: Andy Shellam User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) MIME-Version: 1.0 To: Zdravko Balorda CC: pgsql-sql@postgresql.org Subject: Re: PHP pg_escape_string References: <4A3602F6.5050907@siix.com> In-Reply-To: <4A3602F6.5050907@siix.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200906/72 X-Sequence-Number: 32677 Hi, Zdravko Balorda wrote: > there are both pg_(un)escape_bytea() functions > but only one pg_escape_string()... I wonder if I may be > missing something here? Yeah, I think you are. pg_escape_string (funnily enough) escapes string data which is then stored in the database. You would use this for escaping things like apostrophes in a text field so PostgreSQL wouldn't think the apostrophe in the field is the "end of data" marker. However this string is *not* stored in the database in an escaped form, as it's only escaped for the SQL command, therefore it makes no sense to unescape it. bytea columns on the other hand, are a way of sending and receiving binary data as a textual representation to/from the database server. The data you send and receive is both encoded, therefore you need to unescape it to read it back out. For example a null byte (byte value 0) cannot be sent or received in a SQL command, because a null byte represents an end-of-string in C. Other byte values similarly cannot be sent in a string because they cannot be converted to a character (e.g. ASCII newline/linefeed.) Regards, Andy