duandie0921 2018-01-02 09:39
浏览 93
已采纳

如何使用PDO从SET类型列返回多个值?

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?

  • 写回答

1条回答 默认 最新

  • dongxie9448 2018-01-02 10:23
    关注

    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/>";
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀