0

我在 PHP/MySQL 方面是个菜鸟,到目前为止我已经成功了,但是,现在我需要从两个相关的表中获取信息。我对我需要做的事情做了很多研究,但似乎无法弄清楚我哪里出错了。

问题:如何从 shortlinks 表中获取与 shortlink_analytics 中的短链接相关的链接?

我收到错误:警告:mysql_fetch_array() 期望参数 1 是资源,给定的布尔值

我的表格如下:

短链接表

ID

短链接

关联

创建时间

由...制作

shortlink_analytics 表

短链接

推荐人

命中时间

用户IP

用户代理

到目前为止我尝试过的代码是:

$linecount = 1;
//$mostPop is where I think I have gone wrong and this is where the warning refers to
$mostPop = "SELECT shortlink, COUNT(shortlink) FROM shortlink_analytics JOIN shortlinks ON shortlink_analytics.shortlink = shortlinks.shortlink GROUP BY shortlink ORDER BY COUNT(shortlink) DESC LIMIT 10";
            $loadPop = mysql_query($mostPop);
            echo '<table id="middleIndex">';
            echo '<tr><td class = "overFlow"><h2>Most Popular Shortlink</h2></td></tr>';
            echo '<tr>';
            while($row = mysql_fetch_array($loadPop))
            {
                    echo '<td class = "overFlow">'.$row[1].' visits - <a href = "info.php?link='. $row['shortlink'] .'">hud.ac/' . $row['shortlink'] . '</a></td>';
                    echo '</tr>';
                    $linecount++;
            }
            echo '<tr id="indexMiddle"><td id="hand" class = "overFlow"><a onclick="indexMostPopular()">View More</a></td></tr>';
                echo '</table>';

我相信我对如何使用JOIN或形成成功连接所需的参数没有足够的了解。

4

2 回答 2

1

shortlink在这两个表中都会产生模棱两可的情况。试试这个sql 语句

$mostPop = "SELECT shortlink_analytics.shortlink AS short, 
COUNT(shortlink_analytics.shortlink) AS shortcount,
(SELECT link FROM shortlinks 
WHERE shortlinks.shortlink = shortlink_analytics.shortlink) AS shLink
FROM shortlink_analytics 
JOIN shortlinks ON shortlink_analytics.shortlink = shortlinks.shortlink 
GROUP BY shortlink_analytics.shortlink 
ORDER BY COUNT(shortlink_analytics.shortlink) DESC LIMIT 10";
于 2013-01-09T11:21:14.210 回答
0

试试这个(在 mysql 中运行此查询,如果它正在工作,请将其添加到 php 脚本中)

SELECT 
    shortlink_analytics.shortlink, COUNT(shortlink_analytics.shortlink)
FROM
    shortlink_analytics
INNER JOIN
    shortlinks ON shortlink_analytics.shortlink = shortlinks.shortlink
GROUP BY shortlink_analytics.shortlink
ORDER BY COUNT(shortlink_analytics.shortlink) DESC
LIMIT 10

并检查mysql_effected_rows()函数以检查查询是否会返回某些行

于 2013-01-09T11:18:00.243 回答