我被难住了。为什么查询在 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'
最佳答案
根据我的经验,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 实例。您没有连接到您认为在一个或另一个界面中连接到的数据库。
关于php - 查询在 phpMyAdmin 中有效,但不能通过 mysql_query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1355789/