gpt4 book ai didi

php - MYSQL 结果不显示所有记录

转载 作者:行者123 更新时间:2023-11-29 05:14:04 25 4
gpt4 key购买 nike

我这里有一个简单的脚本,用于从 2 个日期范围内从 mysql 中提取记录。很奇怪它只显示 4 条记录,而有 6 条记录,我没有对记录设置任何限制。谁能帮帮我?

尝试选择:-2月1日至2月27日

这是我的测试站点:http://iamawesome.xyz/track/review.php

enter image description here

这是我的代码:-

$done = 0;
function displayTable($results) {
$total_result = count($results);
echo $total_result." results found.";
for($x = 1; $x <= $total_result; $x++) {
//echo "<tr><td>$allresult['id'][$x]</td>";
echo "<tr><td>".$results['date'][$x]."</td>";
echo "<td>".$results['url'][$x]."</td>";
echo "<td>".$results['clickid'][$x]."</td>";
echo "<td>".$results['code'][$x]."</td></tr>";
}
}

if (isset($_POST['sub'])) {

$datefrom = $_POST['fromdatetime'];
$dateto = $_POST['todatetime'];

if ($datefrom == "From" || $datefrom == "" && $dateto == "To" || $dateto == "") {
echo "ERROR! No date/time selected";
}
else {
/* create a prepared statement */
$stmt = mysqli_prepare($mysqli, "SELECT * FROM data WHERE date_data between (?) AND (?)");

/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ss", $datefrom, $dateto);

/* execute query */
mysqli_stmt_execute($stmt);

/* bind result variables */
mysqli_stmt_bind_result($stmt, $resultid, $resulturl, $resultdate, $resultclickid, $resultcode);

$resultcount = 0;
$allresult = array();

/* fetch value */
while (mysqli_stmt_fetch($stmt)) {
//$allresult['id'][$resultcount] = $resultid;
$allresult['url'][$resultcount] = $resulturl;
$allresult['date'][$resultcount] = $resultdate;
$allresult['clickid'][$resultcount] = $resultclickid;
$allresult['code'][$resultcount] = $resultcode;
$resultcount++;
}

/* close statement */
mysqli_stmt_close($stmt);


/* close connection */
mysqli_close($mysqli);
$done = 1;
}
}
?>

<style>
.searchbox {
border-radius: 20px; 20px; 20px; 20px;
-moz-border-radius: 20px; 20px; 20px; 20px;
-webkit-border-radius: 20px; 20px; 20px; 20px;
border: 2px solid #58ACFA;
}
</style>

<div class="searchbox">

<form action="review.php" method="post" style="margin-left:20px; padding-top:10px;">
<input id="fromdatetime" name="fromdatetime" type="text" placeholder="From" readonly> - <input id="todatetime" name="todatetime" type="text" placeholder="To" readonly>
<input type="submit" name="sub" value="Search" id="btnreview">
</form>
</div>
<br><br>
<?php
if ($done == 1) {
?>
<div class="searchbox">
<table border="1" width="100%" cellspacing="100%">
<tr>
<td><b>Date/Time</b></td>
<td><b>From URL</b></td>
<td><b>ClickID</b></td>
<td><b>Affiliate Code</b></td>
</tr>
<?php
displayTable($allresult);
}
?>
</table>

最佳答案

问题是您正在计算第一个数组。您目前有一个多维数组,您只计算第一个数组有多少个数组。

因为你在此处将 4 个不同的值存储到数组中

$allresult['url'][$resultcount] =  $resulturl;
$allresult['date'][$resultcount] = $resultdate;
$allresult['clickid'][$resultcount] = $resultclickid;
$allresult['code'][$resultcount] = $resultcode;

当您执行 count($allresult) 时,它会输出 4,因为 $allresult包含 4 个子数组,url , date , clickidcode .

然后您使用 for -loop 在你的 displayTable 中输出数组功能。一方面,您从 1 开始计数(而数组从索引 0 开始),但更重要的是,这里的计数仅为 4,因为第一层数组包含 4 个子数组(如上所述)。

我会使用 foreach 重写函数-loop 代替,因为它更适合遍历数组。但是,您可以只更改 count函数通过子数组之一进行计数,方法是将您的函数从

function displayTable($results) {
$total_result = count($results); // Outputs static 4, generated in the while-loop
echo $total_result." results found.";

for($x = 1; $x <= $total_result; $x++) {
// continue here...

function displayTable($results) {
$total_result = count($results['url']); // Actual counts of instances found from the query
echo $total_result." results found.";

for($x = 0; $x < $total_result; $x++) {
// continue here...

这里本质上改变的是 x = 1; $x <= $total_result$x = 0; $x < $total_result而且,最重要的是,来自 $total_result = count($results);$total_result = count($results['url']); .

关于php - MYSQL 结果不显示所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570830/

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