警告:为foreach()提供的参数无效

时间:2015-11-03 09:24:52

标签: php sql pdo

这是我添加的代码,它只是说错误为

  

警告:为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();
?>

但如果我删除&#34; limit 1&#34;在查询中。但那个时间相同的记录重复了3次。什么似乎是问题?

1 个答案:

答案 0 :(得分:4)

limit之前您的查询需要空格且$_SESSION['username']必须在引号

之前
 $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循环

 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/>';
    }