gpt4 book ai didi

PHP/MySQL忽略小数点右边的数字

转载 作者:行者123 更新时间:2023-11-29 02:02:36 27 4
gpt4 key购买 nike

我编写了一段代码,让用户可以从两个选项中选出一个来投票,然后使用修改后的 Elo 评分系统计算分数。但是,当我投票给其中一个选项时,小数点右边的数字将被忽略。我添加了 floatval 函数,但没有帮助。

$query = "SELECT pic,score,id FROM nfl
JOIN (SELECT r1.random_id
FROM nfl_map AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(row_id)
FROM nfl_map)) AS row_id)
AS r2
WHERE r1.row_id >= r2.row_id
ORDER BY r1.row_id ASC
LIMIT 1) as rows ON (id = random_id)";

$query_2 = "SELECT pic,score,id FROM nfl
JOIN (SELECT r1.random_id
FROM nfl_map AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(row_id)
FROM nfl_map)) AS row_id)
AS r2
WHERE r1.row_id >= r2.row_id
ORDER BY r1.row_id ASC
LIMIT 1) as rows ON (id = random_id)";
$res_2 = $mysqli->query($query_2);
if (!$res_2) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_2 = $res_2->fetch_assoc();
$id_2 = $res_2->fetch_assoc();
$score_2 = $res_2->fetch_assoc();


$res_1 = $mysqli->query($query);
if (!$res_1) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_1 = $res_1->fetch_assoc();
$id_1 = $res_1->fetch_assoc();
$score_1 = $res_1->fetch_assoc();
$Ra = $score_1;
$Rb = $score_2;
$calc_pow_1 = (($Rb - $Ra) / 400);
$calc_pow_2 = (($Ra - $Rb) / 400);
$float_calc_pow_1 = floatval($calc_pow_1);
$float_calc_pow_2 = floatval($calc_pow_2);
$Ea = 1 / (1 + pow(10,$float_calc_pow_1));
$Eb = 1 / (1 + pow(10,$float_calc_pow_2));
$float_Ea = floatval($Ea);
$float_Eb = floatval($Eb);

// if user votes for picture no. 1
if (isset($_POST['vote_1']) && isset($_POST['id']) && isset($_POST['score']))
{
$id = $_POST['id'];
/* $score = $_POST['score'];
$pic = $_POST['pic']; */
//$mod = 2 * $Eb;
$K = 4;
$float_mod_1 = floatval($K * (1 - $float_Ea));

$query = "UPDATE nfl SET score=?+$float_mod_1 WHERE id=?";
// $score_new = $_POST['score'] + $mod;
$score_new = $_POST['score'] + $float_mod_1;

$stmt = $mysqli->prepare($query);
$stmt->bind_param('di', $_POST['score'], $_POST['id']);
$stmt->execute();
if ($stmt->errno) {
echo "Vote failed: " . $stmt->error();
}
else echo "Vote succeeded! New score of $id_1 is $score_new!<br/><br/>";
$stmt->close();
}

// fetch picture no. 2
//
// query to the database
/* $query_2 = "SELECT pic,score,id FROM nfl
JOIN (SELECT r1.random_id
FROM nfl_map AS r1
JOIN (SELECT (RAND() *
(SELECT MAX(row_id)
FROM nfl_map)) AS row_id)
AS r2
WHERE r1.row_id >= r2.row_id
ORDER BY r1.row_id ASC
LIMIT 1) as rows ON (id = random_id)";
$res_2 = $mysqli->query($query_2);
if (!$res_2) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_2 = $res_2->fetch_assoc();
$id_2 = $res_2->fetch_assoc();
$score_2 = $res_2->fetch_assoc(); */

// if user votes for picture no. 2
if (isset($_POST['vote_2']) && isset($_POST['id']) && isset($_POST['score']))
{
$id = $_POST['id'];
/* $score = $_POST['score'];
$pic = $_POST['pic']; */
//$mod = 2 * $Ea;
$K = 4;
$float_mod_2 = floatval($K * (1 - $float_Eb));

$query = "UPDATE nfl SET score=?+$float_mod_2 WHERE id=?";
// $score_new = $_POST['score'] + $mod;
$score_new = $_POST['score'] + $float_mod_2;

/* $query = "UPDATE nfl SET score=?+1 WHERE id=?";
$score_new = $_POST['score'] + $mod; */

$stmt = $mysqli->prepare($query);
$stmt->bind_param('di', $_POST['score'], $_POST['id']);
$stmt->execute();
if ($stmt->errno) {
echo "Vote failed: " . $stmt->error();
}
else echo "Vote succeeded! New score of $id_2 is $score_new!<br/><br/>";
$stmt->close();
}

这是完整的代码,如果您需要查看:http://snipt.org/vDhj2

处理用户投票给第二个选项的情况的代码几乎相同(除了 $Ea 更改为 $Eb 等)。 MySQL 中 'score' 行的属性如下:

float(8,3) not null

谢谢。

编辑:我不再收到通知了。

最佳答案

我认为您只是错过了代码中的声明:

$res_1 = $mysqli->query($query);
if (!$res_1) die ("Result display failed: " . $mysqli->errno . " - " . $mysqli->error);
$pic_1 = $res_1->fetch_assoc();
$id_1 = $res_1->fetch_assoc();
$score_1 = $res_1->fetch_assoc();
$Ra = $score_1;
$Rb = $score_2;

我看不到上面代码中任何地方定义的 $score_2,您试图将它的值分配给另一个变量。

警告是有原因的 - 忽略它们是一个坏主意,因为它不可避免地会导致您的代码出错。在这种情况下,您只需在分配变量之前声明您的变量(即使它是 si,ply $score_2=0; 如果这是默认值,但仍然分配它。

关于PHP/MySQL忽略小数点右边的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12181414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com