duankan8739
duankan8739
2016-05-16 05:27
浏览 75
已采纳

使用PDO bindValue将数组插入数据库

I need to insert in one of my tables fields as an array. So I defined a function to convert the input to array like this:

function ConvertToArray($input){
    if( is_array( $input ) ) // for input array type
        return $input ;
    else if( !isset( $input ) || $input == 'undefined' || ($input == "" && $input !==0) || $input == '""' || $input == "''" ){ // for empty input
        $arr = array();
        return $arr ;
    }
    else{  // for string input
        $newarr = array();
        array_push( $newarr, $input );
        return $newarr ;
    }
}

then i want to insert it in database with PDO, but when binding the value i have an error saying:

Array to string conversion error

this is my code:

$usageType = $this -> ConvertToArray($values['UsageType']);
$prepared->bindValue(':UsageType', $usageType); // here is the error

any Idea how to solve my problem? Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqwh1205
    dqwh1205 2016-05-16 05:52
    已采纳

    To store array in database, either use serialize() or convert it to json as per your convenient.

    $prepared->bindValue(':UsageType', serialize($usageType));
    
    点赞 评论

相关推荐