且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP / MySQL:在数据库列中存储多个值

更新时间:2023-01-22 12:19:21

我会说这是个坏主意。如果您打算使用有限的数字类型,例如家庭,办公室,移动电话等,建议您为每个数字添加列。否则,请创建一个单独的数据库表 phone_numbers,并将其与您的客户ID或类似名称相关联,并添加任意数量的号码。搜索将更快,更容易实现。

I would say its a bad idea. If you planning to have limited types of numbers ie Home, Work, Mobile etcetera I suggest you to add columns for each number. Otherwise make a separate database table "phone_numbers" and relate this to your customer id or similar and add as many numbers you like. Searching will just be both faster and more easy to implement.

如果您仍然需要将它们存储在单个字段中,请在使用php implode()和explode()时建议。在我这样做的情况下,我将使用客户很少想到的字符,例如管道符号 |作为分隔符。当然,如果我的用户不小心在输入中使用了str_replace或类似技术,则***使用它来过滤掉,但***是如果很少使用该字符。

If you still need to store them in a single field then use php implode() and explode() as you suggest. In my cases where I do this I would use a character that my customers would rarely think of such as a pipe symbol "|" as a delimiter. Of course I need to use str_replace or such technique to filter out if my users accidentally has used it in the input but best if its a character seldom used.