如何使用 php 返回连接查询的结果?
查询如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用这个查询:
从 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'];
它返回一个数组,以便您可以将其循环出来
It returns an array so you can loop it out
我不知道为什么它不允许我评论 Trikks 的答案,但他基本上是正确的,除了一件事,所以我会把它给他。您需要的不是 $row[0],而是 $row['video_id']; (根据提供的示例)。我将 echo 更改为单引号,因为您没有在 echo 中使用变量,并且单引号比双引号更快。
编辑:另外,如果您没有返回任何值,我会使用
print_r(array_key($row));
检查您是否使用了正确的键: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.
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));
: