如何使用 php 返回连接查询的结果?

发布于 2024-11-14 03:57:01 字数 687 浏览 3 评论 0原文

查询如下:

  SELECT v2.video_id
    FROM `VideoTags` AS v1
    JOIN `VideoTags` AS v2 USING ( tag_id )
   WHERE v1.video_id =1
     AND v1.video_id <> v2.video_id
GROUP BY v2.video_id
ORDER BY COUNT( * ) DESC

我试图获取相关视频,当我在 phpmyadmin 中运行此查询时,它返回正确的结果集,但是当我尝试使用 php 编写此查询并回显结果时,我没有得到任何值,返回了我使用的代码问题是:

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

$row = mysql_fetch_array( $result );

echo "ID ".$row['video_id'];

我确定它很简单,我只是似乎无法弄清楚?如果有人可以帮忙...

The Query is as follows:

  SELECT v2.video_id
    FROM `VideoTags` AS v1
    JOIN `VideoTags` AS v2 USING ( tag_id )
   WHERE v1.video_id =1
     AND v1.video_id <> v2.video_id
GROUP BY v2.video_id
ORDER BY COUNT( * ) DESC

i am trying to get related videos when i run this query in phpmyadmin it returns the correct result set however when i try to write this using php and echo out the results i get no values returned the code im using for the query is:

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

$row = mysql_fetch_array( $result );

echo "ID ".$row['video_id'];

im sure its something simple i just cant seem to figure it out? if someone could please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

死开点丶别碍眼 2024-11-21 03:57:01

尝试使用这个查询:
从 VideoTags AS v1 中选择 v2.video_id 作为 v2id JOIN VideoTags AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC

,然后

echo "ID ".$row['v2id'];

try using this query:
SELECT v2.video_id as v2id FROM VideoTags AS v1 JOIN VideoTags AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC

and then

echo "ID ".$row['v2id'];

居里长安 2024-11-21 03:57:01

它返回一个数组,以便您可以将其循环出来

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

while ($row = mysql_fetch_array( $result )) {
    echo "ID " . $row[0] . "<br />";
}

It returns an array so you can loop it out

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

while ($row = mysql_fetch_array( $result )) {
    echo "ID " . $row[0] . "<br />";
}
一身仙ぐ女味 2024-11-21 03:57:01

我不知道为什么它不允许我评论 Trikks 的答案,但他基本上是正确的,除了一件事,所以我会把它给他。您需要的不是 $row[0],而是 $row['video_id']; (根据提供的示例)。我将 echo 更改为单引号,因为您没有在 echo 中使用变量,并且单引号比双引号更快。

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

while ($row = mysql_fetch_array( $result ))
{
    echo 'ID ' . $row['video_id'] . '<br />';
}

编辑:另外,如果您没有返回任何值,我会使用 print_r(array_key($row)); 检查您是否使用了正确的键:

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

while ($row = mysql_fetch_array( $result ))
{
    print_r(array_key($row));
    echo 'ID ' . $row['video_id'] . '<br />';
}

I'm not sure why it won't let me comment on Trikks's answer, but he's basically correct except for one thing, so I would give it to him. Instead of $row[0], you will want $row['video_id']; (according to the example provided). I changed the echo to single quotes since you're not using variables inside the echo and single quotes are faster than double quotes.

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

while ($row = mysql_fetch_array( $result ))
{
    echo 'ID ' . $row['video_id'] . '<br />';
}

EDIT: Also, if you're not getting any values returned, I would check that you're using the right key using print_r(array_key($row));:

$result = mysql_query("SELECT v2.video_id FROM `VideoTags` AS v1 JOIN `VideoTags` AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

while ($row = mysql_fetch_array( $result ))
{
    print_r(array_key($row));
    echo 'ID ' . $row['video_id'] . '<br />';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文