why are two query's inside one php tag not working

95 Views Asked by At

I have tried out some code were two mysql statement need to be executed, I don't have any syntax error though, but second mysql query is not working and not giving out any results. Please help me out am pretty new to this field if any mistake pardon me, thank you.

.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

There are 3 best solutions below

11
Mani On BEST ANSWER

Use like this, it may work

<?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>';
    }
}
?>
0
Shadow On

Instead of using nested queries and php loops, use join in the sql query, a left join in this case:

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

Although it is unclear what exactly you are trying to achieve in the inner loop.

2
AudioBubble On

I think the best approach is to use LEFT JOIN on your query as below:

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

I hope that one helps