Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id DD934632ADB for ; Sun, 22 Nov 2009 15:22:42 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 27253-03 for ; Sun, 22 Nov 2009 19:22:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from blu0-omc4-s37.blu0.hotmail.com (blu0-omc4-s37.blu0.hotmail.com [65.55.111.176]) by mail.postgresql.org (Postfix) with ESMTP id 15D9263248C for ; Sun, 22 Nov 2009 15:22:31 -0400 (AST) Received: from BLU0-SMTP30 ([65.55.111.135]) by blu0-omc4-s37.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.3959); Sun, 22 Nov 2009 11:22:30 -0800 X-Originating-IP: [67.68.74.190] X-Originating-Email: [racine_89@sympatico.ca] Message-ID: Received: from [192.168.123.102] ([67.68.74.190]) by BLU0-SMTP30.blu0.hotmail.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Sun, 22 Nov 2009 11:22:30 -0800 Date: Sun, 22 Nov 2009 14:22:07 -0500 From: Sylvain Racine User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: pgsql-php@postgresql.org Subject: Use of pg_escape_string() Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 22 Nov 2009 19:22:30.0111 (UTC) FILETIME=[22364EF0:01CA6BA9] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.804 tagged_above=-10 required=5 tests=BAYES_50=0.001, MSGID_FROM_MTA_HEADER=0.803 X-Spam-Level: X-Archive-Number: 200911/1 X-Sequence-Number: 4093 Hello, I use to hear about to escape every variables who come from user in PHP. Most programmers around me use MySQL with mysql_escape_string(). Because I program with PostgreSQL, I take advantage to use pg_escape_string(). Everything goes well, up I entered data with apostrophe('). pg_escape_string() escapes my apostrophe with another apostrophe (''). My data are well store in database. No error... except that appears a double apostrophe. This is not what I want. Maybe something is wrong in my program. Here is a sample of what I use to store data in table "personnes" which have two columns: firstname, lastname. I remove database connection and construction of objects Minute and Personnes. Ex: Nathalie Prud'homme gives Nathalie Prud''homme... $minute->personnes->firstname = $_POST["firstname"]; $minute->personnes->lastname = $_POST["lastname"]; $fields = array("firstname","lastname"); $query = $GLOBALS['phpag']->db->record('personnes',$fields,$minute->personnes); if (!$GLOBALS['phpag']->db->query($query)) { echo $GLOBALS['phpag']->db->error; } class db { function query($query, $offset=0, $num_rows=-1) { if (!$this->link_ID) { $this->error .= '
Can't connect to database.
'; return FALSE; } $this->record = array(); $this->count = 0; if (!$num_rows && $this->debug) { print 'number of lines limit = '.$GLOBALS['phpag_info']['preferences']['phpag']['lines']; $num_rows = $GLOBALS['phpag_info']['preferences']['phpag']['lines']; } if ($num_rows > 0) { $query .= ' LIMIT '.$num_rows.' OFFSET '.$offset; if ($this->debug) { print '
Query: '.$query.'
'; } $this->query_ID = pg_query($this->link_ID, $query); } else { if ($this->debug) { print '
Query: '.$query.'
'; } $this->query_ID = pg_query($this->link_ID, $query); } if (!$this->query_ID) { $this->err = pg_last_error($this->link_ID); $this->error .= '
Error in query sent to database

'.$this->err."

\n"; $this->error .= "Invalid SQL query::
$query

"; return FALSE; } else { for ($i = 0; $i < pg_numrows($this->query_ID); $i++) { $this->record[$i] = pg_fetch_array($this->query_ID,$i); if ($this->debug) { echo 'Record #'.$i.' '; print_r($this->record[$i]); print "
"; } } //Calculate how many records $this->count = ($this->record ? count($this->record): 0); } return $this->query_ID; } // Escape string if necessary function slash ($text) { $text = pg_escape_string($text); return $text; } function record($table,$fields,$values) { if (class_exists(get_class($values))) $values=get_object_vars($values); $query = 'INSERT INTO '.$table.'('; if (empty($champs)) { $arg['type'] = 'php'; $arg['message'] = '
SQL Error: You try to insert values in table without declare column name!
'; Error::thrown($arg,FALSE,TRUE); } foreach ($fields as $num =>$col) { $query .= ($num ? ',': '').$col; } $query .= ') VALUES ('; if (empty($values)) { $arg['type'] = 'php'; $arg['message'] = '
SQL Error: You try to insert data in table without giving values!
'; Error::thrown($arg,FALSE,TRUE); } $valueClause = ''; $id = 0; foreach ($values as $num => $col) { preg_match('/^id/',$num,$match); if (!empty($match[0])) $id += 1; if (empty($match[0])) { // Remove column beginning with 'id...' $valueClause .= ($valueClause ? ',' : '').'\''.$this->slash($col).'\''; } } if (count($fields) != (count($fields) - $id)) { $arg['type'] = 'php'; $arg['message'] = '
SQL Error: The number of columns mismatches with the number of values!
'; Error::thrown($arg,FALSE,TRUE); } $query .= $valueClause.');'; return $query; } } Anybody have an idea?