2013-05-29 127 views
0

我在phpMyAdmin的工作,并适当地返回行的查询:SQL工作在phpMyAdmin但不是在PHP

"select item_number, general_category from products where key_word_i = 'Floor looms' order by item_number" 

在我的PHP代码,它不会失败,但是,不返回行:

$productlist="select item_number, general_category from products where key_word_i = 'Floor looms' order by item_number;"; 
$productrows = mysql_query($productlist) or die("darn it"); 

如果修改查询只搜索“key_word_i ='织机'”,它在两个地方都起作用。为了增加混淆,我有本地产品数据库的副本和生产副本;有问题的原始查询在所有实例的生产数据库中正常工作。

那么我的本地数据库有什么问题?当然,希望这是愚蠢的简单的事情......并且对传统问题表示歉意 - 我正在转向PDO,但还没有和这门课合作。谢谢!

回答

2

,而不是无用'darn it'有害die()变得有用和友好

$res = mysql_query($productlist) or trigger_error(mysql_error()); 

所以,你会被告知发生了什么一定的误差。

还要注意,这段代码不应该返回任何行,而只会返回一个特殊类型的变量 - 一个mysql资源。让行,你必须遍历资源,如本例中的手册页

,如果它仍然不返回行 - 然后有数据库中没有行以符合您的标准

+0

这将有助于如果你可以告诉他,要改变这事后 – samayo

+0

谢谢你,我修改了代码,包括trigger_error,并确认没有错误引发的,简单地说,没有行被返回。另外,我知道结果是一个mysql资源,并且正在迭代“while($ products = mysql_fetch_array($ productrows)){”,它在返回行时正常工作。 – Amos

+0

啊,有时真相会伤害你。没有行。在我的本地环境中,即使在最初连接到正确的数据库的地方,我也有某处切换到错误的过时数据库。对不起,浪费你们大家的时间。看到它简单地表明触发我找到问题。谢谢。 – Amos

2

我敢肯定它是这样的:

"select item_number, general_category 
from products where key_word_i = 'Floor looms' order by item_number;" 

通知如何有嵌套查询分号(;)。

"select item_number, general_category 
    from products where key_word_i = 'Floor looms' order by item_number"; 

是你想要的

+0

+1我很确定,那也是。我没有注意到 – samayo

+0

@phpNoOb是的,我不得不重新阅读几行代码才能注意到它。 – Woot4Moo

+0

不能相信某人低估了我和你 – samayo

相关问题