2015-11-03 121 views
0

这是我所添加的代码,它只是说,误差警告:()提供的foreach无效参数

警告:()提供的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(); 
?> 

但如果我在查询中删除“limit 1”,这可以正常工作。但那次同样的记录重复了3次。什么似乎是这个问题?

+0

您的SQL查询无效,这可能是造成问题。添加一些错误处理并预先检查退货。 –

回答

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

他们还需要引用用户名值,假设它是一个字符串。尽管OP在这种情况下可能应该使用准备好的语句。更新:好得多:) –

+0

它尝试后...>警告:非法字符串偏移 –

+0

@ Jon Stirling, 当它在用户名中添加引号时,它会给出语法错误.. –

相关问题