I discovered now that SET
is different from ENUM
because it allows you to insert multiple values from a list.
So I try to return data from a set column. First of all I create the table "Test_SET_vs_ENUM", than the "ColumnSET"('A','B','C','D') column and after that I inserted in the first row A,B,C
So I try to return data in different ways:
//First try
$sql=$conn->query("SELECT ColumnSET FROM Test_SET_vs_ENUM WHERE IDRow=1");
$result=$sql->fetch();
$conn->exec($sql);
foreach ($result as $content){
echo "Content: ".$content['ColumnSET']."<br/>";
}
And the output is:
Content: A
Content: ,
So I find out that a SET column stored data in form of FirstValue,SecondValue,3Value etc. So when I fetch the result the comma will "end" the array and I will have in the output only the first Value and his comma. This is confirmed by my second try:
//Second try
$sql=$conn->query("SELECT ColumnSET FROM Test_SET_vs_ENUM WHERE IDRow=1");
$result=$sql->fetchAll();
$conn->exec($sql);
foreach ($result as $content){
echo "Content: ".$content['ColumnSET']."<br/>";
}
This time the output was:
Content: A,B,C
But my goal is to have in output each different value, so I tryed this:
//Third try
$sql=$conn->query("SELECT ColumnSET FROM Test_SET_vs_ENUM WHERE IDRow=1");
foreach ($sql as $content){
echo "Content: ".$content['ColumnSET']."<br/>";
}
$conn->exec($sql);
The output is equal to the Second try.
So i'm asking you: How to get the values of a SET column and control each of them separately?
After getting the comma seperated value, you need to explode() it.
Then iterated over this exploded array using foreach() and show them separatly.
Do like below:-
foreach ($result as $content){
$contents_ids = explode(',',$content['ColumnSET']);
foreach($contents_ids as $contents_id){
echo "Content: $contents_id<br/>";
}
}