doudou6719 2015-09-16 10:47
浏览 36
已采纳

将php数组保存到mariadb动态列

I have an array like this

   $test = array(
        'Subscription' => '1',
        'Streaming' => '1',
        'Download' =>  '0'
    )

so during updating to mariaDB

  $query = 'UPDATE table_1 set category_dynamic = COLUMN_CREATE(' . $test . ') where id = 1';
  $this->Model->query($query);

I want to save the array this way ('Subscription',1,'Streaming',1,'Download',0)

any suggestion ?

  • 写回答

2条回答 默认 最新

  • douxuanpa8298 2015-09-16 11:13
    关注

    You can try this :

    <?php
    
    $test = array(
            'Subscription' => '1',
            'Streaming' => '1',
            'Download' =>  '0'
    );
    
    $data = '';
    foreach($test as $key=>$value)
    {
        $data .= '"'.$key.'"'.', '.$value.', ';
    }
    
    $data = rtrim($data,', ');
    
    $query = 'UPDATE table_1 set category_dynamic = COLUMN_CREATE(' . $data . ') where id = 1';
    

    Hope it will solve the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?