来自PHP,SQL查询的奇怪结果

时间:2018-08-21 17:43:05

标签: php sql calculation

我正在做一个与数据库兼容的实验,结果很奇怪。

系统如下所示: 我有一个按钮,每次单击它都会在计数器上加1。 一旦达到一定数量,它将计算分数扣除并更新分数

PHP:

$counter = $_POST['counter'];
$user = $_POST['user'];

$sql = "SELECT score FROM board WHERE player = '$user'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);

if($counter == 1)
  $divisor = 0;
else if($counter == 5)
  $divisor = 0.1;
else if($counter == 20)
  $divisor = 0.3;
else if($counter == 50)
  $divisor = 1;}

$deduction = $row['score'] * $divisor;

$sql = "UPDATE board SET score = score - ".$deduction." WHERE player = '$user'";

我以分数= 10开始 这就是结果

Score: 10
Counter: 1

Divisor = 0; Deduction = 10 * 0;
UPDATE ... score = score - Deduction (0)
New score = 10
-------------------------------------

Score: 10
Counter: 5

Divisor = 0.1; Deduction = 10 * 0.1;
UPDATE ... score = score - Deduction (1)
New score = 9
-------------------------------------

Score: 9
Counter: 20

Divisor = 0.3; Deduction = 9 * 0.3;
UPDATE ... score = score - Deduction (2.7)
New score = 6.3
-------------------------------------

## Here is the curious part

Score: 6.3
Counter: 50

Divisor = 1; Deduction = 6.3 * 1;
UPDATE ... score = score - Deduction (6.3)
New score = 0.000000190735

关于我为什么得到 0.000000190735 的任何想法吗? 我现在有点困惑。

B'rgrds,

1 个答案:

答案 0 :(得分:3)

发表评论的时间有点长。

毫无疑问,这是由 const link = this.windowRefService.nativeWindow.document.getElementById('navigator'); link.setAttribute('href', url); link.click(); itertools.product([ ('object1', 'object2', 'object3'), (1, 2, 3), ('this is 1', 'this is 2', 'this is 3') ]) 的不同表示形式引起的,或者是由score到查询字符串的转换引起的。很难确切指出问题出在哪里,因为有几个不同的地方。

如果您确实需要处理此问题,请在数据库中进行计算:

$divisor

将占位符作为参数传递-而不是查询字符串。