0

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 Answers3

2

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

  • 1
    get back to me in case of any problem. I will give you another solution based on your code –  Jun 08 '16 at 07:29
  • If you wont mind..can you please help me resolve this-> " http://stackoverflow.com/questions/37784065/a-static-data-being-stored-for-every-dynamically-generated-fields-and-submit-but " issue –  Jun 13 '16 at 08:31
0

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64
0

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>';
    }
}
?>
Mani
  • 2,675
  • 2
  • 20
  • 42
  • sure sir..!! ill try it out. –  Jun 08 '16 at 07:10
  • Sorry sir but you used two closing braces? –  Jun 08 '16 at 07:12
  • that will not make any problem, as per your code I edited, if you don't want remove it. – Mani Jun 08 '16 at 07:14
  • Call all the members by name. This will show as we are all same. – Mani Jun 08 '16 at 07:16
  • thank you @ mani can i ask how exactly it works in my case.?? because i never used a join before.! –  Jun 08 '16 at 07:33
  • 1
    I understand what you want exactly, because I suffered like this lot of problems like this. – Mani Jun 08 '16 at 08:13
  • please if you wont mind, can you explain how this joins work..!! any source is also okay, coz i am wanting to learn it.. –  Jun 08 '16 at 08:41
  • Join is nothing but, the connection between two or more tables. check here http://www.mysqltutorial.org/mysql-inner-join.aspx – Mani Jun 08 '16 at 08:45
  • Why do you have the same criteria in the on clause and in the where clause as well? – Shadow Jun 08 '16 at 08:48
  • @Shadow: Because if we not used where , all rows will execute in left side, if it is not matched with right table also left table execute and right table having null value. But Hari need to avoid left table also, that's why we used where condition. – Mani Jun 08 '16 at 08:55
  • If you wont mind..can you please help me resolve this-> " http://stackoverflow.com/questions/37784065/a-static-data-being-stored-for-every-dynamically-generated-fields-and-submit-but " issue –  Jun 13 '16 at 08:31