MySQL查询在PHPMyAdmin中工作但不能在PHP中工作

时间:2014-12-12 09:49:29

标签: php mysql mysqli

我写了一个MySQL查询:

$feedItem->tags =

$result = mysqli_query("SELECT * 
    FROM qzxh_k2_tags, qzxh_k2_tags_xref
    WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
    AND qzxh_k2_tags.id = '406'
    AND qzxh_k2_tags_xref.itemID = '".$item->id"'");

while($tag = mysqli_fetch_array($result))
echo $tag;

查询本身在PHPMyAdmin中显示了我需要的结果,但由于某种原因,在PHP中使用时没有显示任何内容。有什么想法,我出错了吗?

2 个答案:

答案 0 :(得分:3)

您缺少连接运算符(点)

更改

"SELECT * 
    FROM qzxh_k2_tags, qzxh_k2_tags_xref
    WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
    AND qzxh_k2_tags.id = '406'
    AND qzxh_k2_tags_xref.itemID = '".$item->id"'"
// You are missing dot . here                  ^

"SELECT * 
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id."'"

答案 1 :(得分:2)

在sql查询中使用正确的字符串连接。查找以下更正的查询。

$result = mysqli_query("SELECT * 
    FROM qzxh_k2_tags, qzxh_k2_tags_xref
    WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
    AND qzxh_k2_tags.id = '406'
    AND qzxh_k2_tags_xref.itemID = '".$item->id."'");