我正在向数据库中插入一些字段。其中一个字段是电话号码字段。例如,如果您以“01234”开头的数字,它将截断字符串开头的“0”。
我正在使用 mysqli 和准备好的语句将字符串插入数据库,并且我有一个不同的插入查询,该查询不使用准备好的语句,它不会在开头切断“0”,这导致我假设准备好的语句正在修剪“0”。
我还创建了将其插入到 varchar 中的字段,看看是否可以使其工作,但没有运气。
你知道为什么当我使用准备好的语句时它会在开头截掉“0”吗?我该如何解决这个问题?
I am inserting a number of fields into the database. One of the fields is a phone number field. If you start a number with '01234' for example, it will cut off the '0' at the beginning of the string.
I am using mysqli and prepared statements to insert the string into the DB, and I have a different insert query that is not using prepared statements where it does not cut off the '0' at the beginning, which leads me to assume the prepared statement is trimming the '0' off.
I also made the field it is inserting it into a varchar to see if that would make it work, but no luck.
Do you have any idea why it is cutting the '0' off at the beginning when I'm using the prepared statement? How can I get around this?
发布评论
评论(2)
如果您在任何时候将电话号码指定为实际数字(即整数、小数、浮点数等),它将删除所有前导零。
因此,电话号码应作为字符串接受、传输和存储。由于您从未对电话号码进行数学运算,因此它应该不会出现任何问题。
我猜您在准备好的声明或其他步骤中将其指定为数字。
If at any point you are designating the phone number as an actual number (i.e. int, decimal, float, etc) it will remove any leading zeros.
For this reason, phone numbers should be accepted, transferred, and stored as strings. Since you never perform math on a phone number, it should present no problems.
I'm guessing that in your prepared statement or other step you are designating it as a number.
确保在 $types >mysqli_stmt::bind_param。
Make sure to use the correct
$types
in mysqli_stmt::bind_param.