2009年2月23日星期一

Re: [fw-mvc] quote a string without the quote delimiters

OK,
Actually right after i sent the email, i tried

            $sql = "DELETE FROM articles WHERE id IN (". $db->quote($idArray, 'INTEGER') .")" ;

And this gives exactly what i want. Sorry if this is a spam

Cheers,
Hoang

--- On Tue, 2/24/09, Van H Tran <tvhoang1980@yahoo.com> wrote:
From: Van H Tran <tvhoang1980@yahoo.com>
Subject: Re: [fw-mvc] quote a string without the quote delimiters
To: "Hector Virgen" <djvirgen@gmail.com>
Cc: fw-mvc@lists.zend.com
Date: Tuesday, February 24, 2009, 6:01 PM

Thanks Hector

Your solution actually prints
     "DELETE FROM articles where id IN ('23', '43', '12')"

Now, this does delete the rows, HOWEVER, the id (of type Integer) is now treated as string. I don't feel too good about this. I want somethinglike this
    "DELETE FROM articles where id IN (23, 43, 12)"

Regards,
Hoang


--- On Tue, 2/24/09, Hector Virgen <djvirgen@gmail.com> wrote:
From: Hector Virgen <djvirgen@gmail.com>
Subject: Re: [fw-mvc] quote a string without the quote delimiters
To: tvhoang1980@yahoo.com
Cc: fw-mvc@lists.zend.com
Date: Tuesday, February 24, 2009, 1:30 PM

$db->quote() will properly quote an array (by quoting each item in the array) but you still have to add the parenthesis around them:

$idArray = array(23,43,12);
$sql = "DELETE FROM articles where id IN (" . $db->quote($idArray) . ")";
echo $sql; // prints "DELETE FROM articles where id IN (23, 43, 12)"

-Hector


On Mon, Feb 23, 2009 at 6:17 PM, Van H Tran <tvhoang1980@yahoo.com> wrote:
Hi all,

This is what it says about Zend_Db_Adapter quote function:
http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.quoting

Note that the return value of quote() includes the quote delimiters around the string. This is different from some functions that escape special characters but do not add the quote delimiters, for example mysql_real_escape_string().



So if I have
$idList = implode (',', $idArray)
then how do I quote this string $idList (of the form, say, "(23,43,12)") to do this query

$sql = "DELETE FROM articles where id IN " . $db->quote($idList);

it will be come
DELETE FROM articles where id IN ('23,43,12')
This is obviously wrong.

How can I fix this problem with Zend? I don't want to use mysql_real_escape_string() or any non-Zend other function

Thanks,
Hoang


没有评论: