1

Records which contain an ampersand (&) are not returned by MySQL (connected to via PHP), but the same query works fine in phpMyAdmin. I am not sure if it is a collate/charset problem. Here is the sample query:

SELECT distinct(pcs.shop_name) AS shopName 
FROM shops AS ps 
LEFT JOIN category_shop AS cs 
   ON ps.shop_id = cs.shop_id 
WHERE 1=1 
   AND (ps.shop_name LIKE '%P&G%') 
   AND ps.is_active!=2 
ORDER BY ps.shop_display_name ASC

The shop_name I am trying to search for is P&G.

4

1 回答 1

1

试试这个(我猜如果你对 & 有问题,你正在使用 GET 提交表单):

$shop_name = urlencode('P&G');

在您的页面上获取该字段值(这意味着您在生成表单时为每个字段值调用 urlencode)。然后在 GET 完成后,只需执行以下操作:

$shop_name = mysql_real_escape_string($_GET['shop_name']);
$query = "SELECT distinct(pcs.shop_name) as shopName 
    FROM shops AS ps 
    LEFT JOIN category_shop AS cs ON ps.shop_id = cs.shop_id 
    WHERE 1=1 
        AND (ps.shop_name LIKE '%{$shop_name}%') 
        AND ps.is_active!=2 
    ORDER BY ps.shop_display_name ASC";

这对你有用吗?

于 2012-05-21T14:54:40.490 回答