Pawel
Pawel

Reputation: 187

PHP/MySQL ignoring digits to the right of the decimal point

I wrote a code that lets the user vote for one out of the two options and the score is calculated using a modified Elo rating system. However, digits to the right of the decimal point are ignored when I vote for either of the options. I added the floatval function but it didn't help.

$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!

"; $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!

"; $stmt->close(); }

Here is the full code, if you need to see it: http://snipt.org/vDhj2

The code handling the situation when the user votes for the second option is pretty much the same (except $Ea changes to $Eb etc.). The 'score' row's properties in MySQL are following:

float(8,3) not null

Thanks.

EDIT: I'm not getting the notice anymore.

Upvotes: 2

Views: 389

Answers (4)

Pawel
Pawel

Reputation: 187

Okay I found it. It was a stupid mistake with fetch_assoc() etc. Here is the revised code: http://snipt.org/vEaj7

Upvotes: 0

Itai Sagi
Itai Sagi

Reputation: 5615

Getting a notice in PHP isn't an ERROR obviously, it's just ugly and can burden your load...

the notice you're getting means that you're using a non-set variable, doing a var_dump(isset($score_2)) will result in bool(false), meaning that it wasn't set.

PHP is a dynamically typed language, meaning that you don't have to declare variables prior to using them, however - it's a good practise if you do, to avoid such notices which could result in errors you simply need to declare the variable prior to using it.

are you expecting a number? initialize it so

$score_2 = 0;

or

$score_2 = null;

depends on your needs, if you fill it with null, don't forget to allow in mysql NULL values...

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33542

I think you simply missed a declaration in your code:

$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;

I cannot see $score_2 defined anywhere in the code above where you try to assign it's value to another variable.

Warnings are there for a reason - ignoring them is a bad idea as it will inevitably lead to an error in your code. in this case, you simply need to declare your variable before you assign it (even if it is si,ply $score_2=0; if that is to be the default value, but assign it nonetheless.

Upvotes: 1

Kristian
Kristian

Reputation: 21840

Since it's just a notice, does it matter? If so, what should I change in my code?

incorrectly written code that throws an error needs to be fixed.

you cannot assign $Rb = $score_2 before you create the $score_2 variable -- that is an error. put them in the right order to solve that problem

Upvotes: 1

Related Questions