0

I have a problem when comparing two PHP values. The first value is being passed as a parameter in the query string, while the second parameter is a result of an sql query.

The logic I need here is to compare both values, so that if the user has enough credit, his payment can be processed, but otherwise, an error message is shown.

This is my code:

public function storeNewTicket($userId, $amount, $validity) {

        $currentBalance = $this->getBalance($userId);
        $floatCurrentBalance = floatval($currentBalance);
        $floatAmount = floatval($amount);


        if ($floatCurrentBalance > $floatAmount) {

            /*Proceed with the insert*/
        }
        else {
            echo "You don't have enough credit to process this payment."
        }
    }

In a seperate function in the same file called getBalance(), I am getting the current balance of the user from a different table and returning it as an object. I am 100% sure that this is working. Here is the code:

public function getBalance($userId) {

    $stmt = $this->conn->prepare("SELECT balance FROM userbank WHERE UserId = ?");    
    $stmt->bind_param("s", $userId);

    if ($stmt->execute()) {
        $bal = $stmt->get_result()->fetch_assoc();
        $stmt->close();
        return $bal['balance'];
    } else {
        return NULL;
    }
}

The code is always going through the else clause and echoing: You don't have enough credit to process this payment.;

Can someone please help me understand how to convert the variables to decimals or floats and compare them?

18
  • 2
    You say that the return value from getBalance is an object ( array? ) - yet you use $floatCurrentBalance=floatval($currentBalance) ~ surely if it is an object/array then you would need to access the value contained within ? Commented Jan 13, 2016 at 14:36
  • 3
    As above, $currentBalance is an array. You need to access the specific array key, something like $floatCurrentBalance=floatval($currentBalance['balance']) Or better yet have getBalance actually return a float and not just the database row as an array Commented Jan 13, 2016 at 14:38
  • 2
    $balance = $stmt->get_result()->fetch_assoc() ~ that will return an array not a single value. From the manual "mysqli_result::fetch_assoc -- mysqli_fetch_assoc — Fetch a result row as an associative array" Commented Jan 13, 2016 at 14:44
  • 2
    unless fetch_assoc() is your own method, on a userland class, and not the php built in method, then $currentBalance certainly is an array, probably with a single element. Why not var_dump($this->getBalance($userId)) and check for yourself Commented Jan 13, 2016 at 14:46
  • 1
    $currentBalance['balance'] will access the float. I suggest you change it in the getBalance() function thoughj: return $balance['balance']; For good measure, you can rename the variable used in that function: $row = $stmt->get_result()->fetch_assoc(); $stmt->close(); return $row['balance']; The code will function the same, but it wont be as confusing for future you when you come back to make changes Commented Jan 13, 2016 at 14:52

2 Answers 2

2

Please use the BC Math functions when working with floats. For comparing floats use bccomp.

Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand, -1 otherwise.

Beware that the third parameter is the number of digits after the decimal place.

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

1 Comment

Although this could be a valid answer, the whole process can be done in an easier way using basic PHP code ;)
0

I found where the problem actually was. My getBalance() method was actually returning the value 1 when being converted to a float. 1 was the position of the record in the array. Changing the declaration of $floatCurrentBalance to: $floatCurrentBalance = floatval($currentBalance['balance']);

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.