I’d like to take data from a field which is a string of words separated by commas and save each of those words to a variable. After which, I’d like to take the stored words and use them to retrieve information.
An example of this would be a field containing the information: “Bob,Sue,Adam,Tim,Hank” My goal is to select each name, using the commas to detect each different name, and save each name to a variable or, more efficiently, an array, then use a “for” command or similar to display relevant info about each person.
I’m using MySQLi commands, which I have been told are poorly documented; however, that is what my book instructed me to use.
The
explodefunction the other answers suggest will work fine, but it would be more elegant if you stored these names separately in a second table.So, where you now have:
main:id,namesI suggest:
main:idnames:main_id,nameYou can then retrieve the names belonging to one row with a query such as:
To add a name belonging to a certain id, use: