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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)