简体   繁体   English

如何将逗号分隔的数据库值导入数组

[英]How to import comma separated database values into an array

Rather then creating a db column for each value, I want to include them into one field, and then call those comma separated values into a PHP array to be called individually later. 而不是为每个值创建一个db列,我想将它们包含在一个字段中,然后将这些逗号分隔的值调用到一个PHP数组中,以便稍后单独调用。

Example; 例; db value for 'data_set' is; 'data_set'的db值是; 111, 222, 333 111、222、333

$sql_data = $get['data_set'];

$data_set = array ( $sql_data ); 

I want $data_set[0] to return 111, $data_set[1] to return 222, etc 我希望$ data_set [0]返回111,$ data_set [1]返回222,依此类推

Instead, it returns the entire db value, indicating that the import is not recognizing the comma separated values. 而是,它返回整个db值,指示导入未识别逗号分隔的值。

How can I fix this? 我怎样才能解决这个问题? Or is there a better approach? 还是有更好的方法?

I highly suggest using a one to many or many to many design for this rather than cramming the values into a single field. 我强烈建议为此使用一对多或多对多的设计,而不是将这些值填入单个字段中。 Doing this will cause issues in the future and maintenance will be a nightmare. 这样做会在将来引起问题,并且维护将是一场噩梦。 If you provide a bit more information about what kind of data you're working with (What do the numbers represent? What uses the numbers?) I'd be happy to illustrate a more usable design. 如果您提供有关正在使用的数据类型的更多信息(数字代表什么?数字使用什么?),我很乐于说明一个更有用的设计。

That said, if you insist on having multiple values in a single field, you can use the PHP function explode() : 也就是说,如果您坚持在一个字段中包含多个值,则可以使用PHP函数explode()

$data_set = explode(',', $sql_data);

Updated Answer Based On Comment 根据评论更新答案

In that case, there are a few ways you could do this. 在这种情况下,您可以通过几种方法来执行此操作。 If the properties are always in the same order and represent the same thing (and there are only 5-6), I'd recommend just breaking them out into separate columns and naming the columns appropriately. 如果属性始终按相同的顺序表示相同的事物(只有5-6个),则建议将它们分成单独的列,并适当命名这些列。

If there are a lot of potential properties and they're variable, I'd recommend creating a profiles table, a properties table (just id and name), and a profilesToProperties table. 如果有很多潜在的属性并且它们是可变的,那么我建议创建一个配置文件表,一个属性表(仅id和name)和profilesToProperties表。 That will allow the most flexible adjustments to properties over time. 这样可以随时间对属性进行最灵活的调整。 To get all properties for a given profile, you'd do something like: 要获取给定配置文件的所有属性,您可以执行以下操作:

SELECT prof.username, prop.name
FROM profiles prof
    LEFT JOIN profilesToProperties ptp ON prof.id = ptp.profile_id
    LEFT JOIN properties prop ON prop.id = ptp.property_id
WHERE prop.id = :id

This would still return profiles with zero properties. 这仍然会返回属性为零的配置文件。

What it can be used fore: Quiz webpage where every question have for example 4 different choice 1-4. 它可以用来做什么:测验网页,其中每个问题都有4个不同的选择1-4。 The user goes to next question (program extracts numbers from field in db (4,4,2,4,1,3)) representing previous question answered, ads the number from present question to the string and update the field in db (4,4,2,4,1,3,1) With 50 questions it will otherwise be 50+ columns in the table for answers. 用户转到代表下一个已回答问题的下一个问题(程序从db(4,4,2,4,1,3)中的字段中提取数字),将当前问题的编号添加到字符串中,并更新db(4中的字段,4,2,4,1,3,1)如果有50个问题,则答案表中将有50多个列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM