When to use each data type of mysqli_stmt_bind_param

4

According to documentação :

  

i - corresponding variable has type integer - d - corresponding variable has type string
b - corresponding variable is a blob and will be sent in   packets

So I wanted to know when exactly to use each of these types, which characters each of these types accepts.

The question arose because in MySQL database I use VARCHAR to save currency values, and I always associated VARCHAR with string , so I was passing coin values as s , but I saw that certain is with d , because the bank was saving without . and without , . Does s only accept letters? Double only accepts numbers and . and , ? What is b ? Anyway, a general and up-to-date explanation would be cool. Thanks.

    
asked by anonymous 25.09.2015 / 22:18

1 answer

4

As stated by @rray b is used for binary data, for example photos, zipped files, etc.

The types VARCHAR and TEXT are fields that use string or accept from A to Z from 0 to 9, so use must be s , do not use to do calculations. >

Then the supported characters vary depending on the collection used. For example:

  • utf8_general_ci supports unicode characters however it is simpler.
  • utf8mb4_unicode_ci supports most (if not all) unicode characters.
  • latin1_general_ci does not use unicode, but supports accents.

That is, s you can pass a string, but when the process arrives in the query the characters can be lost if they are not compatible with collection used.

Working with Monetary Values

If you are going to use monetary values, maybe it is best to use DECIMAL (note that you will not use comma ).

See this answers (although talking about sql-server): link

The decimal type

The decimal type does not have the same accuracy problems as float and double types. It is a kind of fixed point, not floating. In other words, if you define a field in a database as MySQL of type DECIMAL(10,2) , it means that it will be a 10-digit number (in base 10), 2 of which will be after the comma. Ever. Some databases also internally store such data as strings.

This storage strategy eliminates the problem of data rounding, but on the other hand, it reduces the type's flexibility. While it may not be a good choice for data where the number of decimal places can not be set, it is a good choice for example for financial figures (since these always have 2 decimal places).

Note also that in some cases, decimal-type values will be converted to floating-point numbers implicitly when used in calculations. In this case if the expected result also of the decimal type, it will be rounded and converted from float to decimal. That is, accuracy is better, but it is not unlimited.

Source: link

    
25.09.2015 / 22:37