RE: [PHP-DB] odbc query with single quote in string
| From: | John W. Holmes | Date: | Wed, 05 Feb 2003 00:42:29 +0000 |
| Subject: | RE: [PHP-DB] odbc query with single quote in string | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-25440@lists.php.net to get a copy of this message | ||
> I'm having problem querying a table with a field value containing an
> appostropy, please help. Using ODBC to connect MSAcess database.
>
> $mQuery = "CustomerID='$mCust'";
> $mCur2 = odbc_do( $mCnx, "select Login from Emails where $mQuery"
);
>
> When it hits "O'Donald, James", I get error in odbc_exec(). I tried
> variations of $mQuery, including:
>
> $mQuery = addslashes( $mQuery );
You don't want to use addslashes() on $mQuery, you want to use it on the
data you're inserting between the quotes, i.e. $mCust.
$mQuery = "CustomerID='" . addslashes($mCust) . "'";
If that still causes an error, your database may require quotes to be
escaped with another quote, instead of a backslash. In that case, you
can create a function like this
function addslashes2($data)
{ return str_replace("'","''",$data); }
Hope that helps.
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/