如何在while循环中从mysqli_fetch_array()中检索当前和下一个元素?

时间:2018-06-07 07:42:55

标签: php html mysql

我有一张表可以有多个相同的记录。所以如果有多个记录相同,我想做一些任务。在这种情况下,我该如何检查呢。

<div>
<table class="table">

<thead class="table table-bordered" style="background-color:#EBF5FB;">
  <tr>
    <th>Name</th>
    <th>Locations</th>
    <th>Type</th>
    <th>Number</th>
    <th>Users</th>
    <th>Edited</th>
    <th>ID</th>
  </tr>
</thead>

<div class="pre-scrollable">
<tbody id="myTable">

 <?php
        include_once 'Connection.php';
        $check_test=mysqli_query($GLOBALS['db'],"select * from test ORDER bY id DESC");

        while ($row = mysqli_fetch_array($check_test)) {
            echo '<td>'.$row['name'].'</td>';
            echo '<td>'.$row['region'].'</td>';
            echo '<td>'.$row['number'].'</td>';
            echo '<td>'.$row['rollno'].'</td>';
            echo '<td>'.$row['created'].'</td>';
            echo '<td></td>';
            echo '</tr>';
        }   
    ?>
</tbody>
</div>

</table>
</div><br/>

表行示例:

'genarate id', 'us-west-4', 'Test', '1', '15', '2018-06-06 23:43:02','93'

'genarate id', 'us-east-3', 'Test', '1', '15', '2018-06-06 23:43:02', '93'

如果行是这样的,我需要打印一次这样的记录,如

'genarate id', 'us-east-3 , us-west-4', 'Test', '1', '15', '2018-06-06 23:43:02',  '93'

2 个答案:

答案 0 :(得分:0)

不是在php中,而是在sql中,您更改查询以执行此操作:

SELECT name, email, address, rollno, count(*) as nb
FROM test
GROUP BY name, email, address, rollno
ORDER BY id DESC

然后在php中你只需要显示数字:

 while ($row = mysqli_fetch_array($check_test)) {
            echo $row['name'];
            echo $row['email'];
            echo $row['address'];
            echo $row['rollno'];
            echo $row['nb'];
 }  

自您发表评论以来,您正在寻找的是GROUP_CONCAT,这是查询:

SELECT GROUP_CONCAT(name) as names, email, address, rollno
FROM test
GROUP BY email, address, rollno
ORDER BY id DESC

由于你没有在你想要连接的字段上写字,我选择了名字,但如果你也改变了这个组,你可以为任何一个列做。 要了解有关函数group_concat的更多信息,请参阅手册: group concat

答案 1 :(得分:0)

 var counts = ["2017-01-01T00:00:00Z", 0, "2017-02-01T00:00:00Z", 0, "2017-03-01T00:00:00Z", 0, "2017-04-01T00:00:00Z", 0, "2017-05-01T00:00:00Z", 0, "2017-06-01T00:00:00Z", 0, "2017-07-01T00:00:00Z", 0, "2017-08-01T00:00:00Z", 0, "2017-09-01T00:00:00Z", 0, "2017-10-01T00:00:00Z", 0, "2017-11-01T00:00:00Z", 0, "2017-12-01T00:00:00Z", 0, "2018-01-01T00:00:00Z", 0, "2018-02-01T00:00:00Z", 0, "2018-03-01T00:00:00Z", 0, "2018-04-01T00:00:00Z", 18, "2018-05-01T00:00:00Z", 482, "2018-06-01T00:00:00Z", 272]
 
 var array = [];
 for(var i=0;i<counts.length/2;i++){
  var obj = {date:counts[i*2],id:counts[i*2+1]};
  console.log(obj);
  array.push(obj);
 }
 console.log(array);

或者如果您想知道哪条记录是重复的,那么 - 你可以使用这个SQL查询

You can get the distinct records by using SELECT DISTINCT statement.

<?php
    include_once 'Connection.php';
    $check_test=mysqli_query($GLOBALS['db'],"select DISTINCT name, email, address, rollno from test ORDER bY id DESC");

    while ($row = mysqli_fetch_array($check_test)) {
        echo $row['name'];
        echo $row['email'];
        echo $row['address'];
        echo $row['rollno'];
    }   
?>