查询可以在phpMyAdmin中运行,但不能通过mysql_query运行

时间:2009-08-31 04:31:52

标签: php mysql phpmyadmin

我很难过。为什么查询在phpMyAdmin中有效但在MAMP环境中无效?我已经回显了查询,它在MAMP中返回0结果,将其复制并粘贴到phpMyAdmin中,同样的查询返回预期的结果。

select
   c.id
from
    (select id, business_id 
     from coup 
     where match(name) against ('$search')
    ) as c
    left join
       (select business_id 
        from bus 
        where match(name, category, subcat) against ('$search')
       ) as b on
        c.business_id = b.business_id 
    inner join bloc z on
        c.business_id = z.business_id
where
    z.zip = '$zip'

以下是代码中出现的行

$q = "select c.id from (select id, business_id from ".TBL_COUPONS." where match(name) against ('".$search."')) as c left join (select business_id from ".TBL_BUSINESS." where match(name, category, subcat) against ('".$search."')) as b on c.business_id = b.business_id inner join ".TBL_BLOCATION." as l on c.business_id = l.business_id where l.zip = '".$zip."'";
$rs = mysql_query($q) or die(mysql_error());
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE ){
    $rec[] = $row[0];   
}

echo $ q显示:

select c.id from (select id, business_id from coupons where match(name) against ('walk')) as c left join (select business_id from business where match(name, category, subcat) against ('walk')) as b on c.business_id = b.business_id inner join buslocations as l on c.business_id = l.business_id where l.zip = '91326' 

1 个答案:

答案 0 :(得分:2)

根据我的经验,问题是您使用两个包含不同数据的不同数据库的可能性为80%。

您应该将查询分解为更简单的测试,直到您找到不同的内容。

在两种环境中尝试以下查询:

SELECT COUNT(*) FROM coupons;
SELECT COUNT(*) FROM coupons WHERE MATCH(name) AGAINST ('walk');

SELECT COUNT(*) FROM business;
SELECT COUNT(*) FROM business WHERE MATCH(name, category, subcat) AGAINST ('walk');

SELECT COUNT(*) FROM bloc;
SELECT COUNT(*) FROM bloc WHERE zip = '91326';

这些查询中的一个或多个查询的结果可能会有所不同。这意味着您有两个具有不同数据的不同数据库。同一个MySQL实例上的两个模式,或者两个独立的MySQL实例。您没有连接到您认为在一个或另一个界面中连接的数据库。