What mysqli value for bind_param() i or s

前端 未结 2 1647
灰色年华
灰色年华 2021-01-21 11:17

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 \"73732

相关标签:
2条回答
  • 2021-01-21 11:19

    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).

    0 讨论(0)
  • 2021-01-21 11:41

    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.

    0 讨论(0)
提交回复
热议问题