explode () and mySQL query

explode () and mySQL query

am 29.05.2008 02:13:11 von Ron Piggott

I am trying to add a search feature to my shopping cart.

I am wanting to use PHP to develop the query for mySQL. I have the
following code figured out:

foreach(explode(" ", $keyword) as $key) $query .= " $key, ";

This produces:

WHERE shopping_cart_product.product_description IN ( Jesus, is, Lord, )

The weakness is the trailing , after the last word the user types in.

How can I eliminate this?

Ron


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: explode () and mySQL query

am 29.05.2008 02:21:14 von dmagick

Ron Piggott wrote:
> I am trying to add a search feature to my shopping cart.
>
> I am wanting to use PHP to develop the query for mySQL. I have the
> following code figured out:
>
> foreach(explode(" ", $keyword) as $key) $query .= " $key, ";

I hope this isn't a copy/paste from your code as it's going to cause
problems with sql queries (put a ' in one of your keywords and see what
happens). If it is, you need to use mysql_real_escape_string.

If it's not, then ignore :P

> This produces:
>
> WHERE shopping_cart_product.product_description IN ( Jesus, is, Lord, )
>
> The weakness is the trailing , after the last word the user types in.

The easiest way would be to do something like this:

$keyword_search = '';
foreach(explode(" ", $keyword) as $key) {
$keyword_search .= "'" . mysql_real_escape_string($key) . "', ";
}

// take off the last 2 chars - one is a 'space' and the other is the
last ','
$keyword_search = substr($keyword_search, 0, -2);

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: explode () and mySQL query

am 29.05.2008 02:52:30 von Ron Piggott

I am not sure why this isn't bringing any results:

WHERE shopping_cart_product.product_description IN ('straying')

The word straying is in the midst of one of the descriptions.

Ron

On Wed, 2008-05-28 at 20:13 -0400, Ron Piggott wrote:
> I am trying to add a search feature to my shopping cart.
>
> I am wanting to use PHP to develop the query for mySQL. I have the
> following code figured out:
>
> foreach(explode(" ", $keyword) as $key) $query .= " $key, ";
>
> This produces:
>
> WHERE shopping_cart_product.product_description IN ( Jesus, is, Lord, )
>
> The weakness is the trailing , after the last word the user types in.
>
> How can I eliminate this?
>
> Ron
--
ron.piggott@actsministries.org
www.actsministrieschristianevangelism.org

Acts Ministries Christian Evangelism
"Where People Matter"
12 Burton Street
Belleville, Ontario, Canada K8P 1E6

In Belleville Phone : (613) 967-0032
In North America Call Toll Free : (866) ACTS-MIN
Fax: (613) 967-9963


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: explode () and mySQL query

am 29.05.2008 03:37:09 von dmagick

Ron Piggott wrote:
> I am not sure why this isn't bringing any results:
>
> WHERE shopping_cart_product.product_description IN ('straying')
>
> The word straying is in the midst of one of the descriptions.

Ahh, oops - didn't notice that before.

"IN()" looks for specific entries, so unless the entry is "straying" and
*only* "straying" it will not be found.

You can either use full text indexes
(http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html ) or "like"
searches:


where description like '%straying%' or description like '%another word%';

full-text works best on a big, random database (if you have a lot of
keywords that are the same it won't work very well).

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: explode () and mySQL query

am 04.06.2008 08:11:50 von Goltsios Theodore

O/H Ron Piggott ??????:
> I am trying to add a search feature to my shopping cart.
>
> I am wanting to use PHP to develop the query for mySQL. I have the
> following code figured out:
>
> foreach(explode(" ", $keyword) as $key) $query .= " $key, ";
>
> This produces:
>
> WHERE shopping_cart_product.product_description IN ( Jesus, is, Lord, )
>
> The weakness is the trailing , after the last word the user types in.
>
> How can I eliminate this?
>
> Ron
>
>
>
You can alternative use implode which I think is a much better choice
because it does exactly what you need.
You can use it like this

$keys = implode(",",$keyword);
$query .= $keys;

This is the reference in the manual:
http://gr2.php.net/manual/en/function.implode.php

--
Thodoris


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: explode () and mySQL query

am 04.06.2008 08:19:32 von dmagick

Thodoris wrote:
> O/H Ron Piggott ??????:
>> I am trying to add a search feature to my shopping cart.
>>
>> I am wanting to use PHP to develop the query for mySQL. I have the
>> following code figured out:
>>
>> foreach(explode(" ", $keyword) as $key) $query .= " $key, ";
>>
>> This produces:
>>
>> WHERE shopping_cart_product.product_description IN ( Jesus, is, Lord, )
>>
>> The weakness is the trailing , after the last word the user types in.
>>
>> How can I eliminate this?
>>
>> Ron
>>
>>
>>
> You can alternative use implode which I think is a much better choice
> because it does exactly what you need.
> You can use it like this
>
> $keys = implode(",",$keyword);
> $query .= $keys;

Not really - because it'll create an invalid sql query. Even if you put
quotes around it, it would be prone to sql injection (search for o'malley).

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php