What mysqli value for bind_param() i or s

依然范特西╮ 提交于 2020-06-08 19:59:08

问题


I am using mysqli bind_param() when I'm inserting stuff to my DB.

When I tried to insert a phone number as "0737327373" the value got inserted as "737327373" without the leading "0". But when I changed bind_param("i", $phoneNumber) to bind_param("s", $phoneNumber) instead the value got inserted correctly as "0737327373". So should I use "s" instead of "i" when I'm inserting numbers with a leading 0 in it?

Also, I have a value that could be anywhere between (0-5) and (0-1) depending on what select/checkboxes a user check during a registration.

Should those values also be inserted as bind_param("s") since they could be "0" as well.


回答1:


If you are inserting a "number" into a varchar field then yes, use an "s".

In your case it makes no real sense to insert a phone number as an integer since you probably won't be doing any math analysis on it and you lose functionality (leading zeros, for example).




回答2:


You should use bind_param("s", $phoneNumber) 99% of the time. The other types are there only for exceptional situations where the correct type is necessary for the query to work.

Important: The binding type should be dictated by the column/operand type in SQL not by the data you have. It doesn't matter if your value is a number or a string, what matters is in what context it is used in SQL.

In your situation you claim that your phone number starts with a zero, but an integer cannot start with a zero unless it is a zero itself. It's mathematically wrong. Phone numbers are not integers! They are strings. You should store a phone number in a VARCHAR column in your database.



来源:https://stackoverflow.com/questions/19796240/what-mysqli-value-for-bind-param-i-or-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!