-3

我已经有大约 2 天的错误了,如果有人可以帮助我,我会很感激。

目标: 我想做一个查询,选择一个月内的所有付款并将它们保存在一个数组中,我想绘制这个图表,所以我还需要没有付款的日子。

这是我的代码,我认为我的问题是当我尝试在数组中捕获它们时。当我从我的主页调用我的函数时,我的所有网站都关闭了。

 function getDailyGraph($membership, $selectedMonth){
       $sql = "SELECT rate_amount AS payment, DAY(date) AS day FROM payments WHERE membership_id = ".$membership.
      " AND MONTH(date) = ".$selectedMonth." ORDER BY day";
      $query = $db->query($sql);
      $days = array();
      for ($i=1; $i <= 31 ; $i++) { 
        $payment = mysql_fetch_array($query);
        # code...
        if ($i == $payment['day']) {
          # code...
          $days[] = $payment['payment'];
        }else{
          $days[] = 0;
        }
      }
        return $days;
  }
4

2 回答 2

0

first of all, if you are using PDO, your code should be something like

$sql = 'SELECT rate_amount AS payment, DAY(date) AS day FROM payments WHERE membership_id = ".$membership." AND MONTH(date) = ".$selectedMonth." ORDER BY day';
foreach ($db->query($sql) as $payment) {
    //do something with $payment['dia'], $payment['payment'], etc...     
}

if you are using mysl_* functions, you should consider mysqli_* and your code should look like

$sql = 'SELECT rate_amount AS payment, DAY(date) AS day FROM payments WHERE membership_id = ".$membership." AND MONTH(date) = ".$selectedMonth." ORDER BY day';

/* connection to database */
$link = mysqli_connect("localhost", "my_user", "my_password", "my_database");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Select */
if ($result = mysqli_query($link, $sql)) {
    //work with your data here
    while($row = mysqli_fetch_array($result)){
        //do something with $payment['dia'], $payment['payment'], etc...
    }

    //free memory
    mysqli_free_result($result);
}else{
    echo "Error in query";
}
于 2013-09-27T23:36:49.467 回答
0

您似乎有一个无限循环;你$i+1应该是$i+=1or $i++,所以你的循环永远不会完成,这将导致页面永远不会完成/加载。

e:我发的太早了。

您的代码中也存在逻辑错误;您正在选择日子,但无论如何您都在增加;如果仅在 10 日、15 日和 17 日付款,那么当您的循环位于 1、2、3 并且它们不可能匹配时,您将使用直径 10、15、17 拉起三行。

分别进行查询和日期匹配。

只要您从结果数组中获取结果mysql_fetch_array并存储结果数组,就执行一个循环;也许在另一个按天索引的地方,然后遍历它并检查您实际可用的天数。

于 2013-09-27T23:09:09.140 回答