gpt4 book ai didi

php - 显示mysql数据时出现重复列

转载 作者:行者123 更新时间:2023-11-29 21:04:56 27 4
gpt4 key购买 nike

我正在使用 php 从 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();
}
?>

最佳答案

首先,请尝试将代码更改为 MySQLi,现在对于您的问题,您需要知道 mysql_fetch_array 将返回一个具有关联键和数字键的数组。来自 docs :

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

因此,为了避免循环中出现重复数据,您需要使用以下三个选项之一:

// 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);

关于php - 显示mysql数据时出现重复列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36904455/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com