doutao1171 2016-02-23 06:59
浏览 149
已采纳

MYSQL结果不显示所有记录

I have a simple script here just to extract the record from mysql from the 2 date range. It is weird it just show 4 records while there is 6 records, I did not set any limit to the records. Can anyone help me out?

Try selecting from:- 1st February till 27th February

Here is my testing site: http://iamawesome.xyz/track/review.php

enter image description here

Here is my code:-

$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>
  • 写回答

2条回答 默认 最新

  • dongqin1167 2016-02-23 07:45
    关注

    The problem is that you are counting the first array. You currently have a multidimensional-array, and you're only counting how many arrays the first array has.

    Because you store 4 different values into the array here

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

    when you then do count($allresult), it would output 4, because $allresult contains 4 sub-arrays, url, date, clickid and code.

    You then use a for-loop for outputting the array in your displayTable function. For one thing, you start counting at 1 (while arrays start with the index 0), but more importantly, the count here is only 4, because the first-tier array contains 4 sub-arrays (as discussed above).

    I would rewrite the function using a foreach-loop instead, as that's more suitable for looping through arrays. However, you can just change so that the count function counts through one of the sub-arrays instead, by changing your function from

    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...
    

    to

    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...
    

    What essentially was changed here was x = 1; $x <= $total_result to $x = 0; $x < $total_result and, most importantly, from $total_result = count($results); to $total_result = count($results['url']);.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog