我这里有一个简单的脚本,只是为了从2日期范围中提取mysql的记录。奇怪的是它只显示4条记录,而有6条记录,我没有对记录设置任何限制。任何人都可以帮助我吗?
尝试选择: - 2月1日至2月27日
这是我的测试网站:http://iamawesome.xyz/track/review.php
这是我的代码: -
$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>
答案 0 :(得分:3)
问题是你在计算第一个数组。您目前有一个多维数组,而您只计算第一个数组有多少个数组。
因为您在此处将4个不同的值存储到数组中
$allresult['url'][$resultcount] = $resulturl;
$allresult['date'][$resultcount] = $resultdate;
$allresult['clickid'][$resultcount] = $resultclickid;
$allresult['code'][$resultcount] = $resultcode;
当您执行count($allresult)
时,它会输出4,因为$allresult
包含4个子数组,url
,date
,clickid
和{{ 1}}。
然后使用code
循环在for
函数中输出数组。首先,你从1开始计数(而数组以索引0开始),但更重要的是,这里的计数只有4,因为第一层数组包含4个子数组(如上所述)。
我会使用displayTable
- 循环重写函数,因为它更适合循环遍历数组。但是,您可以更改以便foreach
函数通过其中一个子数组进行计数,方法是更改函数
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);
。
答案 1 :(得分:0)
更新您的查询
SELECT * FROM data WHERE date_data between (?) AND (?)
用
SELECT * FROM data WHERE DATE(date_data) between (?) AND (?)
它可能会奏效。 假设您的第一个参数日期低于第二个参数日期