MYSQL在PHP中返回空结果 [英] MYSQL returns empty result in PHP

查看:125
本文介绍了MYSQL在PHP中返回空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下tableCountry

I have the following tableCountry

country  clicks
-------  ------
0        222
66       34 
175      1000
45       650

然后我使用以下MYSQL语句基于clicks列(仅一个结果)来获得该国家/地区的排名

And I use the following MYSQL statement to get the ranking of any of the country based on the clicks column (just one result)

SELECT COUNT(*) rank 
FROM countryTable a 
JOIN countryTable b 
   ON a.clicks <= b.clicks 
WHERE a.country = 45

以上将返回"2".然后在我的php代码中,我尝试使用

The above will return '2'. Then in my php code I try to access the rank value with

$row =  mysql_fetch_array($result) or die(mysql_error()); 
echo $row['rank'];

但是,如果国家/地区排名第一,则不会返回任何结果.即a.country = 175

But this doesn't return any result if the country is the number one. i.e a.country = 175

推荐答案

ON联接是列之间的联接,而不是比较.

A join ON is a join between columns, not a comparison.

已更新

SELECT COUNT(*)+1 rank 
FROM countryTable
WHERE clicks > (SELECT clicks FROM countryTable WHERE country = 45)

原因:搜索排名是指搜索具有点击次数>给定点击次数的记录数.

Reasoning: searching for a rank mean searching for the number of records that has clicks > a given click.

因此,对于175个国家,有0个点击率更高的国家=>排名1,国家45,有1个点击率更高的国家=>排名2

Therefore, for 175 there is 0 country with better click => rank 1, country 45, 1 country with better click => rank 2

PHP

$result = mysql_query("....")
$row = mysql_fetch_array($result)
...

通常,它应该可以工作,除非您在连接服务器时遇到问题.那是您应该使用调试技能的地方.做一个var_dump($ result)看看它是否返回false,如果是,则是一个连接问题(检查mysql_connect或其他东西)

Normally it should work unless you got a problem with connecting to the server. That's where you should use your debugging skill. Do a var_dump($result) to see if it return false, if yes then it's a connection problem (check mysql_connect or something)

这篇关于MYSQL在PHP中返回空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆