冲突FOREACH MySql SELECT PDO

时间:2015-01-23 06:36:47

标签: php mysql pdo

如何修复foreach mysql PDO中的冲突 我尝试使用其他循环函数但没有给出结果,比如while.this my script

mysql PDO查询,如下所示:

<?php
function calldata($table){
$db = dbConn::getConnection();

$sql = "select * from ".$table."" or die(mysql_error());
$q = $db->query($sql) or die("failed!");
while($res = $q->fetch(PDO::FETCH_ASSOC)){
            $record = array_map('stripslashes', $res);
            $db->records[] = $record; 
        }
        return $db->records;;
    //else echo "No records found";
  } 
 ?>

并显示结果,如下所示:

<table>
<tr><td>Category</td><td>
<select>
<option value="0">- select -</option>
<?php
        try{
        $db = dbConn::getConnection();
        $table='str_prod_category';
        $recordscat = calldata($table);
        if(count($recordscat)){ 
         foreach($recordscat as $key1=>$Recordcat){
        ?>
        <option value="<?=$Recordcat['id_category'];?>"><?=$Recordcat['cat_name'];?></option>
        <?php }}}catch (PDOException $e) {
//Output error - would normally log this to error file rather than output to user.
echo "Connection Error " . $e->getMessage();
} ?>
</select>
</td></tr>
<tr><td>Brand</td><td>
<select>
<option value="0">- Select -</option>
<?php   try{
        $db = dbConn::getConnection();
        $table1='str_prod_brand';
        $recordsbrand = calldata($table1);
        if(count($recordsbrand)){
         foreach($recordsbrand as $key2=>$Recordbrand){
        ?>
        <option value="<?=$Recordbrand['id_brand'];?>"><?=$Recordbrand['brand_name'];?></option>
        <?php }}}catch (PDOException $e) {
//Output error - would normally log this to error file rather than output to user.
echo "Connection Error " . $e->getMessage();
} ?>
</select>
</td></tr>
</table>

这段代码是工作和分配数据但是对于选项品牌,有空选择,然后是空数据后的正确数据。

如何解决这个问题?我尝试将循环从foreach更改为while但没有给出结果。

感谢

1 个答案:

答案 0 :(得分:0)

我尝试使用过滤器使用if和&#s?来删除空白重新编码

foreach($recordsbrand as $key2=>$Recordbrand){
if($Recordbrand['brand_name'] == ''){}else{
<option value="<?=$Recordbrand['id_brand'];?>"><?=$Recordbrand['brand_name'];?></option>
}}

但是,我不知道是好还是坏。

相关问题