我已经出现了大约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;
}
答案 0 :(得分:0)
你似乎有一个无限循环;您的$i+1
应该是$i+=1
或$i++
,因此您的循环永远不会完成,这会导致页面永远不会完成/加载。
您的代码中也存在逻辑错误;你在选择日子,但无论如何你都在增加;如果仅在10日,15日和17日进行支付,那么当你的循环在1,2,3上时,你将用dia 10,15,17拉出三行,并且它们不可能匹配。
分别进行查询和日期匹配。
只要从mysql_fetch_array
获得结果,就执行一个循环并存储生成的数组;也许在另一个按日索引,然后循环,并检查你实际可用的日子。
答案 1 :(得分:0)
首先,如果您使用PDO,您的代码应该类似于
$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...
}
如果你使用mysl_ *函数,你应该考虑mysqli_ *,你的代码看起来应该是
$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";
}