When we do PHP prepare statement, we need to specify bind type such as i, s, d, b. What difference does it make if I bind a number as "s" string?
It makes a difference in how the database driver (i.e. php) sends the data to your DBMS. When binding a type the data is first cast to that specific type and then sent along the wire to your DBMS. For example, say the value you have in PHP is the string "9223372036854775808"
. If you bind it as a string the data is sent along to the DBMS as a string and the DBMS is free to do whatever internal casting it needs to pack and store the value under the specified column type. However, if you bind it as an integer PHP is the one to do the cast first, before sending it over the wire. This means the value first becomes (int) "9223372036854775808"
which results in the value 9223372036854775807
and is then packed as binary payload of 0xffffffff
. The result is the DBMS will accept the data as is without further changes.
This, of course, creates different side effects. For example, integer size (32 bit vs 64 bit vs arbitrary precision) can vary depending on who controls the cast and pack operations. Further more, the onus on checking for integer overflow/wrap precision falls on the side of who does the cast and pack (the driver versus the DBMS). Additional side effects include payload size that goes over the wire. A string is larger than a packed binary integer.
Ideally, specificity is a good thing in this case. Because mostly the database can only communicate with php by sending it everything as a string. Though the driver can make smarter decisions about how to represent that data inside of PHP (which should matter to the programmer) given a more specific representation of how this data is bound between the two ends.