Vladimir
Vladimir

Reputation: 1622

Type of MySQL Decimal value in PHP

I am using BC Math library in PHP, as you know data provided to BC functions MUST BE strings, otherwise very weird results might come out.

I am storing decimal values (18,10) in MySQL.

My questions are:

  1. What is the type of these values when i fetch them in PHP, are they string? and can i use them directly in BC such as bcmul($database_decimal_value , '1', '10') ?

  2. Can i use output of BC functions (which is string i guess) and insert in MySQL decimal type directly?

P.S. i have read the MySQL reference and it says: Values for DECIMAL columns no longer are represented as strings that require 1 byte per digit or sign character. not sure if this means they are no longer strings.

Edit:

I test the answer with the following code:

<?php
$var = 0.00001;
echo bcmul('10', "$var", '5');
?>

But it echoes 0 instead of 0.00010 ! if i change $var = 0.00001 to $var = '0.00001'; (which is string) it works

Upvotes: 2

Views: 778

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

Regardless of whether the value itself is a string or a number, you can force it to be passed as a string using proper quotes using strval()

bcmul(strval($database_decimal_value) , '1', '10');

And yes, you can pass those values to MySQL without quotes and it will treat them as numbers

Upvotes: 2

Related Questions