0

I'm trying to perform some calculation on Data retrieved from database , but evertime I run this code I get NAN as output.

function calculateNPS()
    { 
        $queryTotal = "SELECT count(*) as total FROM message";
        $resullTotal = mysqli_query($link ,$queryTotal);

        $queryYes= "SELECT count(visit) as  yes FROM message WHERE visit='Yes'";
        $resultYes = mysqli_query($link ,$queryYes);

         $queryNo = "SELECT *count(visit) as no FROM message WHERE visit='No'";
        $resultNo = mysqli_query($link ,$queryNo);

        $yescount = mysqli_fetch_assoc($resultTotal);
        $nocount=mysqli_fetch_assoc($resultNo);
        $totalcount=mysqli_fetch_assoc($resultYes);

        $nps = ((float)($yescount['yes'])/(float)($totalcount['total'])*100)-((float)($nocount['no'])/(float)($totalcount['total'])*100);
        echo $nps;

}
4
  • 2
    SELECT *count(visit) is invalid. You'll want to echo/log the results for each to make sure they contain what you think. Commented Jun 2, 2020 at 19:13
  • @aynber corrected that a while ago, but still no change in output. Commented Jun 2, 2020 at 19:16
  • 2
    NaN sounds like it's coming from Javascript, not PHP. As I mentioned, you'll need to double-check the values of each variable by logging them or using echo/var_dump to see what they contain so you can pinpoint the issue. Commented Jun 2, 2020 at 19:22
  • @aynber PHP can display NAN when necessary. it even has a NAN keyword. Commented Jun 2, 2020 at 19:38

2 Answers 2

1

I hope thats just a test-script, because there are a lot of issues you could fall into ;-)

but your problem: i guess its just a typing-mistake. you name the result-var "resullTotal", below you are requesting another var called "resultTotal" ;-)

Sign up to request clarification or add additional context in comments.

Comments

0

Some numeric operations can result in a value represented by the constant NAN. This result represents an undefined or unrepresentable value in floating-point calculations. So it seems you have some problems with the values in the $nps calculation.

I would suggest that you try outputting/inspecting all the params, like $yescount or $yescount['yes'] to make sure you have valid data to start with. Then break your expression into shorter operations until you find the issue.

For instance, mysqli_fetch_assoc returns an associative array that corresponds to the fetched row or NULL if there are no more rows.

Also, in your queries you use $link but I can't see where it's coming from.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.