我正在尝试编写代码来计算sqlite查询返回的用户数,然后在这些用户之间拆分“账单”,但由于某种原因,我的计数器不会增加,我得到了一个除法零错误。我的代码在这里:
<?php
session_start();
require 'database.php';
$db = new Database();
$name = $_POST['name'];
$amount = $_POST['amount'];
$due = $_POST['due'];
$gid = $_GET['gid'];
$uid = $_SESSION['id'];
$count = 0;
$users = $db->query("select * from members where(gid='$gid');");
while($data = $users->fetchArray()) {
$count++;
}
$amount = $amount / $count;
while($data = $users->fetchArray()) {
if($data['uid'] == $uid) {
continue;
} else {
$temp = $data['uid'];
$db->exec("insert into bills values(NULL,'$gid','$uid','$temp','$amount','$due','false','$name');");
}
}
header('Location:grouppage.php?gid='.$gid.'');
?>
任何人对如何解决这个问题都有任何想法?
答案 0 :(得分:0)
无论循环看起来不起作用,你都不能有两个while(... fetchArray())
循环而没有重置内部指针。
但是,在这种情况下,您不需要:
$users = $db->query("select * from members where(gid='$gid')");
$count = $users->numRows();
if( $count == 0) die("No users found!");
$amount /= $count;
while($data = $users->fetchArray()) {
...
}
答案 1 :(得分:0)
当select没有返回任何记录时,你不处理大小写。
看看你的代码:
$users = $db->query("select * from members where(gid='$gid');");
while($data = $users->fetchArray()) {
$count++;
}
未选择数据 - 没有身体 执行,$ count没有增加并且保持为零,因此跟随行中的“除以零”错误。
$amount = $amount / $count;