在另一个查询中使用mysql_query结果

时间:2010-06-28 15:30:13

标签: php multidimensional-array

以下是我正在执行的查询。但由于某些原因,$ MachineQuery没有得到结果。我认为它与在查询中使用$ q1有关,但我不知道为什么。 任何想法?

$q1 =mysql_query("SELECT DISTINCT SymptomID
 FROM Tree
 WHERE SymptomID NOT IN (SELECT DISTINCT SymptomID
                         FROM  Tree
                         WHERE LBNodeID = 0 OR RBNodeID = 0)")

$MachineQuery = mysql_query("SELECT DISTINCT M.MachineID, M.MachineName
                                 FROM Machines M, Systems Sys, Symptoms Sym
                                 WHERE M.MachineID = Sys.MachineID AND Sys.SystemID  
                                 =Sym.SystemID AND Sym.SymptomID IN ($q1)

3 个答案:

答案 0 :(得分:2)

问题是$q1是PHP资源。它不是查询,数组,字符串或其他任何东西。因此,您需要将数据“转换”为更适合您的类型。

这是我的解决方案:

$q1 =mysql_query("SELECT DISTINCT SymptomID
    FROM Tree
    WHERE SymptomID NOT IN (SELECT DISTINCT SymptomID
                         FROM  Tree
                         WHERE LBNodeID = 0 OR RBNodeID = 0)");
$list = array();
while ($row = mysql_fetch_array($q1)) {
    $list[] = $row[0];
}
$fullList = join(',', $list);

$MachineQuery = mysql_query("SELECT DISTINCT M.MachineID, M.MachineName
                                 FROM Machines M, Systems Sys, Symptoms Sym
                                 WHERE M.MachineID = Sys.MachineID AND Sys.SystemID  
                                 =Sym.SystemID AND Sym.SymptomID IN ($fullList)

这样您可以重复使用结果,而不必再次运行查询。

答案 1 :(得分:1)

$ q1是MySQL资源变量,而不是字符串或数组。请参阅文档,了解如何从该资源获取所需的特定字段:

http://us2.php.net/mysql_query

http://us2.php.net/manual/en/function.mysql-fetch-array.php

答案 2 :(得分:0)

我试试

$MachineQuery = mysql_query("SELECT DISTINCT M.MachineID, M.MachineName FROM Machines M, Systems Sys, Symptoms Sym WHERE M.MachineID = Sys.MachineID AND Sys.SystemID
=Sym.SystemID AND Sym.SymptomID IN (SELECT DISTINCT SymptomID FROM Tree WHERE SymptomID NOT IN (SELECT DISTINCT SymptomID FROM Tree WHERE LBNodeID = 0 OR RBNodeID = 0))