0

我是编码新手,并试图制作一个作为 cron 运行的脚本。总的来说,我需要从一个表中找到特定类型的用户才能在第二个表中找到 ID。这是针对大型网站脚本上的会计任务,因此无法通过表格更改任何内容。

用户表列如下 id--用户名--密码--email--已删除

Deleted 是 0 或 1 以及我们将要搜索的列。

交易表列如下 id--时间--owner--banker--type--amount--reason

所以我需要拉出每个没有被删除的唯一用户(a 0)并完成他们的所有交易(所有者)并将它们全部加起来(金额)。对结果执行一些数学运算,然后为该用户创建一个新事务。

到目前为止,这就是我所得到的,也从 webscript 中的一些代码做一些黑客工作。

<?php

$con=mysqli_connect("localhost", "username", "password", "database"); 

$uniqeMembers = mysqli_query($con,"SELECT DISTINCT id FROM users WHERE deleted='0'");

while ($id = $uniqueMembers->fetchRow())
    {
    $playerCreds = $playerCreds = getCol("SELECT SUM(amount) FROM transactions WHERE owner='$id' LIMIT 1");
    $interest = $playerCreds * .05;
    mysqli_query($con,"INSERT INTO transactions (time, owner, banker, type, amount, reason) VALUES ($TIMEMARK, $id, 14, 0, $interest, 'Interest')");
    }


mysqli_close($con);


?>

现在,代码似乎没有做任何事情。

有什么帮助吗?还是更简单的方法?

4

2 回答 2

0

您的代码中有很多错误,但我以这种方式修复了它:

$con = mysqli_connect("localhost", "root", "root", "animals"); 
if(!$con){
    die("Error");
}
$uniqeMembers = mysqli_query($con,"SELECT DISTINCT id FROM users WHERE deleted='0'");

while ($id = $uniqeMembers->fetch_row())    {
    $transaction = mysqli_query($con, "SELECT SUM(amount) FROM transactions WHERE owner='".$id[0]."' ");
    $playerCreds = mysqli_fetch_array($transaction);
    $interest = $playerCreds[0] * .05;
    //mysqli_query($con,"INSERT INTO transactions (time, owner, banker, type, amount, reason) VALUES ($TIMEMARK, $id, 14, 0, $interest, 'Interest')");
    }


mysqli_close($con);
于 2013-07-07T21:46:50.877 回答
0

可能最容易将其作为单个插入来完成,使用选择作为插入的数据源。像这样的东西(未测试)

<?php

$con=mysqli_connect("localhost", "username", "password", "database"); 

$uniqeMembers = mysqli_query($con,"INSERT INTO transactions (time, owner, banker, type, amount, reason) 
                SELECT '$TIMEMARK', users.id, 14, 0, (SUM(transactions.amount)) *0.05, 'Interest'
                FROM users 
                INNER JOIN transactions
                ON users.id = transactions.owner
                WHERE users.deleted='0'
                GROUP BY users.id");

mysqli_close($con);

?>
于 2013-07-07T21:40:58.957 回答