为什么一个php标签内的两个查询无法正常工作

时间:2016-06-08 06:33:51

标签: php mysql

我试过一些代码,两个mysql语句需要执行,虽然我没有任何语法错误,但是第二个mysql查询不起作用而且没有给出任何结果。如果有任何错误原谅我,请帮助我这个领域的新手,谢谢。

.PHP

<?php
$con = mysqli_connect("localhost", "*****", "*****", "******");
$query = ("SELECT * FROM profile");
$result = mysqli_query($con, $query);
while ($row = $result->fetch_assoc()) {
    $query1 = ("SELECT vault_no FROM  grp_tbl");
    $result1 = mysqli_query($con, $query1);
    while ($row1 = $result1->fetch_assoc()) {
        if ($row1['vault_no'] !== $row['vault_no']) {
            echo
            '<div class = "chat-user-name"><span class="pull-right label label-primary"><input name="ppl" type="radio" value= "' . $row["via"] . '" "></span><div align="center"><input type="hidden" name="category" value="macro">
                                                </div>
                                                ' . $row['via'] . ' </div>';
        }
    }
}
?>

3 个答案:

答案 0 :(得分:5)

我认为最好的方法是在查询中使用LEFT JOIN,如下所示:

$query = "SELECT * FROM profile LEFT JOIN grp_tbl ON profile.vault_no <> grp_tbl.vault_no ORDER BY profile.YourOrderField";

我希望有人帮助

答案 1 :(得分:0)

在sql查询中使用join而不是使用嵌套查询和php循环,在这种情况下使用左连接:

select profile.*
from grp_tbl
left join profile on profile.vault_no<>grp_tbl.vault_no
order by profile.via

虽然目前还不清楚你在内循环中想要达到的目的是什么。

答案 2 :(得分:0)

像这样使用,它可能会起作用

<?php
$con = mysqli_connect("localhost", "*****", "*****", "******");
$query = ("SELECT profile.* from profile left join grp_tblon profile.vault_no!=grp_tbl.vault_no where profile.vault_no!=grp_tbl.vault_no");
$result = mysqli_query($con, $query);
while ($row = $result->fetch_assoc()) {
            echo
            '<div class = "chat-user-name"><span class="pull-right label label-primary"><input name="ppl" type="radio" value= "' . $row["via"] . '" "></span><div align="center"><input type="hidden" name="category" value="macro">
                                                </div>
                                                ' . $row['via'] . ' </div>';
    }
}
?>