【问题标题】:Creating an array dynamically and inserting in MySQL DB through PHP动态创建数组并通过 PHP 插入 MySQL DB
【发布时间】:2013-11-25 08:53:26
【问题描述】:

我想创建一个数组,像这样传递给插入查询:

$data=array(
'db_field1'=>$value,
'db_field1'=>$value,
'db_field1'=>$value,
and so on
);    
$this->db->insert('table',$data)      //codeigniter syntax

但我想提前创建数组,然后在匹配条件时继续传递键值对:

if(cond==true)
'"'.$key.'"=>'.$value.',' //pass this to the data array every time the condtion is true

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    你可以试试这个方法:

    $data['db_field1'] = $value;
    if(cond==true){
    $data[$key] = $value;
    }
    

    等等…………

    然后运行插入

    $this->db->insert('table',$data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 2011-01-27
      • 2015-09-16
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多