mysql 2表,从2个表中获取结果

时间:2013-01-06 16:39:24

标签: php mysql

我有2张桌子

1 table = files

      code                     | title

luLidwhSl8hmN0T6RsLaDmxAB09UZcX |这是RAR标题

4Xwvm1C3yTQJK7CnmxorUDI7sNSvcBK |这是JPG标题

...

2 table = hits

      page_name                                | hits

download.php?code = luLidwhSl8hmN0T6RsLaDmxAB09UZcX | 102

download.php?code = 4Xwvm1C3yTQJK7CnmxorUDI7sNSvcBK | 87

...

我的查询是:

            include('db.inc.php');
            $query = mysql_query("SELECT t1.code, t1.title, RIGHT(t2.page_name, 31) as t2.page_name, t2.hits FROM files t1 INNER JOIN hits t2 ON t1.code= RIGHT(t2.page_name, 31) as t2.page_name ORDER by t2.hits DESC LIMIT 1, 7");

            while ($result = mysql_fetch_assoc($query)) {
                echo ' <div id="linkstyle"><strong><a href="http://localhost/edu/filesupload/download.php?code='. $result['t1.code'] . ' ">' , $result['t1.title'] , '</a></strong><br></div>';
                }

我收到此错误

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Edu\filesupload\index.php on line 104

问题出在哪里?

2 个答案:

答案 0 :(得分:1)

改变这个:

$query = mysql_query("SELECT t1.code, t1.title, RIGHT(t2.page_name, 31) as t2.page_name, t2.hits FROM files t1 INNER JOIN hits t2 ON t1.code= RIGHT(t2.page_name, 31) as t2.page_name ORDER by t2.hits DESC LIMIT 1, 7");

对此:

$query = mysql_query("SELECT t1.code, t1.title, RIGHT(t2.page_name, 31) as t2.page_name, t2.hits FROM files t1 INNER JOIN hits t2 ON t1.code= RIGHT(t2.page_name, 31) as t2.page_name ORDER by t2.hits DESC LIMIT 1, 7") or die(mysql_error());

然后那应该告诉你错误是什么,因为如果它没问题,它就不会返回false。

答案 1 :(得分:1)

尝试使用此查询:

SELECT t1.code, t1.title, RIGHT(t2.page_name, 31) as page_name, t2.hits FROM files t1 INNER JOIN hits t2 ON t1.code= RIGHT(t2.page_name, 31) ORDER by t2.hits DESC LIMIT 1, 7