存储在 mysql 整数字段中时允许数字以 0 开头

发布于 2024-09-01 00:16:00 字数 119 浏览 7 评论 0原文

我需要存储以 0 开头的电话号码,但每当我尝试将其存储在 MySql 表中时,开头的 0 就会被删除,因为实际上没有数字以 0 开头。

如何解决这个问题?我需要将字段类型从 Integer 更改为其他类型吗?

I need to store phone numbers starting with 0 but whenever i try to store this in MySql table the starting ZERO is removed because no number start with Zero actually.

How to solve this issue? Do I need to change the field type from Integer to another type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

筑梦 2024-09-08 00:16:41

您还可以使用函数将所需的数字用前导零括起来。我创建了这个函数,如果“字符串”小于 2 位数字,则添加前导零(它用于向小时和分钟添加前导零)

function leadZero($num) {
    if (strlen($num) < 2) {
        return "0" . $num;
    } else {
        return $num;
    }

}

如果你说,你想输出一个数字 2 作为 02,你会做 LeadZero(2);

如果数字长度小于 2 位,则只会添加零!例如leadZero(14);将返回 14

You can also wrap the number you want with a lead zero with a function. I made this function to add lead zero if the "string" is smaller than 2 digits (it was used to add lead zeroes to hours and minutes)

function leadZero($num) {
    if (strlen($num) < 2) {
        return "0" . $num;
    } else {
        return $num;
    }

}

If you have say, a number 2 that you want to output as 02, you'd do leadZero(2);

This will only add a zero IF the number is less than 2 digits long ! For instance leadZero(14); will return 14

﹂绝世的画 2024-09-08 00:16:29

是的 - 数字字段仅存储数字值,而不存储这些值的格式(带有前导零的 paddin 是)。您应该

  1. 将字段类型从整数更改为 varchar 或 char(如果位数始终相同)。

  2. 将数字存储为整数,但根据需要在表示层中添加 0。

Yes - numeric fields only store the numeric values, not the formatting of those (which paddin with leading zeroes is). You should either

  1. change the field type from integer to varchar or char (if # of digits is ALWAYS the same).

  2. Store the number as integer BUT prepend 0 in your presentation layer as needed.

紙鸢 2024-09-08 00:16:12

电话号码不是整数,您只会在尝试将它们存储为整数时遇到问题,而是将它们存储为字符串。

Phone numbers aren't integers and you will only end up with problems trying to store them as integers, store them as strings instead.

南冥有猫 2024-09-08 00:16:10

您可以使用 varchar 数据类型来解决此问题。

You can use data type as varchar to solve this.

潇烟暮雨 2024-09-08 00:16:09

电话号码也可以包含其他符号以提高可读性...电话号码的正则表达式类似于 [0-9+-()*#]+。因此,您需要使用电话号码的文本字段以及一些验证。

Phone numbers can contain other symbols for readability too... a regexp for a phone number looks something like [0-9+-()*#]+. So you need to use a text field for phone numbers, plus some validation.

难以启齿的温柔 2024-09-08 00:16:08

电话号码并不是真正的数字,因为它们不是序数。它们只是字符——事实上它们是数字是偶然的。

将它们存储在 varchar 中并继续:D

Phone numbers are not really numbers in the sense that they aren't ordinal. They're just characters - they fact that they are numbers is incidental.

Store them in a varchar and move on :D

玉环 2024-09-08 00:16:07

将数据类型更改为 unsigned-zerofill 无论您使用什么,float、int、decimal(6,2)...只需将字段编辑为 unsigned-zerofill

change data type to unsigned-zerofill whatever you are using, float, int, decimal(6,2)... only edit the field to unsigned-zerofill

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文