【问题标题】:Return data from Database where data is repeated [duplicate]从重复数据的数据库返回数据[重复]
【发布时间】:2020-10-10 03:24:25
【问题描述】:

很抱歉打扰你们,但我一直在尽我所能从数据库中获取一些数据,但它一直显示我是空白的,所以我也得到了空白闪烁 :)

所以我需要做的是,

正如您在图片上看到的,我有一个包含重复数据的列 ownerId,这没关系,我就是这样工作的,现在我想从数据库中提取的是显示 itemId 并计算 ownerId = 的位置。

所以我的查询如下。

<table id="zctb" class="display table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
    <tr>
            <th>#</th>
            <th>Item Name</th>
            <th>Item ID</th>
            <th>Quantity</th>
    </tr>
</thead>

<tbody>

<?php   
$sql = "SELECT ownerId, itemId, count from user_item where ownerId = :editid";
$query = $dbh2 -> prepare($sql);
$query->bindParam(':editid',$editid,PDO::PARAM_INT);
$query->execute();
$result=$query->fetch(PDO::FETCH_ASSOC);
$cnt=1; 

if($query->rowCount() > 0){
    foreach($results as $result){               
?>  
            <tr>
                <td><?php echo htmlentities($cnt);?></td>
                <td><?php echo htmlentities($result->item_name);?></td>
                <td><?php echo htmlentities($result->itemId);?></td>
                <td><?php echo htmlentities($result->count);?></td>
            </tr>
<?php 
        $cnt=$cnt+1; 
    }
} 
?>

        </tbody>
    </table> 

但这不会返回任何东西,甚至没有在表中找到数据。

有什么想法吗?

【问题讨论】:

  • 你检查了吗,如果 $editid 里有东西

标签: php mysql return


【解决方案1】:

1) 检查变量 $editid 中是否有值

2) 遍历数组时出错: foreach ($results as $result) // 你没有 $results 变量

【讨论】:

  • 是的,你的回答我解决了这个问题。需要像这样 准备($sql); $query->bindParam(':editid',$editid,PDO::PARAM_INT); $查询->执行(); $results=$query->fetchAll(PDO::FETCH_OBJ); $cnt=1; if($query->rowCount() > 0) { foreach($results as $result) { ?>
猜你喜欢
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
相关资源
最近更新 更多