0
votes

This is the code i have added and it just say error as

Warning: Invalid argument supplied for foreach()

<?php
include '../../config/Database.php';
$pdo = Database::connect();
$q = "select branch_add,branch_Address,branch_landNo , branch_email , Fname from db_thisurienterprice.tbl_employee , db_thisurienterprice.tbl_branch , db_thisurienterprice.tbl_login where employeeBranch = branch_ID and NIC = " .$_SESSION['username'] ."limit 1";

foreach ($pdo->query($q) as $row) {

    echo '<label class="control-label">'.$row['Fname'].' </label> <br/>';
    echo '<label class="control-label">'.$row['branch_add'].' </label> <br/>';
    echo '<label class="control-label">'.$row['branch_Address'].' </label> <br/>';
    echo '<label class="control-label">'.$row['branch_landNo'].' </label> <br/>';
    echo '<label class="control-label">'.$row['branch_email'].' </label> <br/>';


}
Database::disconnect();
?>

But this works without error if i remove "limit 1" in the query. but that time same record duplicated at 3 times. What seems to be the issue?

1
You SQL query is invalid which is probably causing the problem. Add some error handling and check the returns beforehand. - Jonnix

1 Answers

4
votes

Your query need space before limit and $_SESSION['username'] must be in quotes

 $q = "select branch_add,branch_Address,branch_landNo , branch_email , Fname from db_thisurienterprice.tbl_employee , db_thisurienterprice.tbl_branch , db_thisurienterprice.tbl_login where employeeBranch = branch_ID and NIC ='" .$_SESSION['username']."' limit 1";

You need to fetch date from your result set then use foreach loop

 foreach ($pdo->query($q) as $row) {

        echo '<label class="control-label">'.$row['Fname'].' </label> <br/>';
        echo '<label class="control-label">'.$row['branch_add'].' </label> <br/>';
        echo '<label class="control-label">'.$row['branch_Address'].' </label> <br/>';
        echo '<label class="control-label">'.$row['branch_landNo'].' </label> <br/>';
        echo '<label class="control-label">'.$row['branch_email'].' </label> <br/>';
    }