2016-04-28 118 views
0

我正在用php生成一个程式化的表格,但由于某些原因,它会创建我的Date和Count的重复列。有人可以帮助弄清楚为什么这发生在我身上?谢谢显示mysql数据时重复列

<?php 

include("dbconnect.php"); 

$link=Connection(); 

$result = mysql_query(
         "SELECT Date, Count 
         FROM testLocation 
         WHERE Date 
         BETWEEN '2016-04-10 00:01:11' AND '2016-04-23 00:01:11'" 
         ,$link 
        ); 


    if($result!==FALSE){ 
     echo '<table cellpadding="0" cellspacing="0" class="db-table">'; 
     echo '<tr><th>Date</th><th>Count</th></tr>'; 
     while($row = mysql_fetch_array($result)) { 
       echo '<tr>'; 
       foreach($row as $key=>$value1){ 
       echo '<td>', $value1,'</td>'; 
       } 
       echo '<tr>'; 
     } 
     echo '</table><br />'; 
     mysql_free_result($result); 
     mysql_close(); 
     } 
?> 
+0

尝试在你的SQL中使用'DISTINCT'。 – Blank

+0

嗨里诺,我不幸已经尝试过,它没有工作。 – amiao

+0

from docs - ['mysql_fetch_array()'](http://php.net/manual/en/function.mysql-fetch-array.php) - * - 将结果行作为关联数组,数组, 或两者*。默认值都是(即'MYSQL_BOTH'),所以你得到了关联数组和数组数组。 ($ row = mysql_fetch_array($ result,MYSQL_ASSOC)){'** OR **'while($ row = mysql_fetch_array($ result)){'to' ){' – Sean

回答

0

首先关闭所有,请尝试更改您的代码库MySQLi,现在你的问题,你需要知道,mysql_fetch_array将返回数组与关联和数字键。从docs

返回字符串数组对应于所提取的行,或FALSE如果没有更多的行。返回数组的类型取决于如何定义result_type。通过使用MYSQL_BOTH(默认),您将得到一个包含关联和数字索引的数组。使用MYSQL_ASSOC,您只能获得关联索引(如mysql_fetch_assoc()工作),使用MYSQL_NUM,您只能获得数字索引(如mysql_fetch_row()工作)。

因此,为了避免在循环重复的数据,你需要为使用这三个选项:

// The important part here is to set the result type eg: MYSQL_ASSOC or MYSQL_NUM. By defualt it is MYSQL_BOTH and thats your problem right now. 
$row = mysql_fetch_array($result, MYSQL_ASSOC | MYSQL_NUM); 

// Or this, it will return rows with associative indexes (keys are named after the column), eg: $row['Date'] 
$row = mysql_fetch_assoc($result); 

// Or this, it will return rows with numeric indexes, eg: $row[0] 
$row = mysql_fetch_row($result);