-1

我需要在我的一个表字段中插入一个数组。所以我定义了一个函数来将输入转换为数组,如下所示:

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 ;
    }
}

然后我想用 PDO 将它插入到数据库中,但是在绑定值时我有一个错误说:

数组到字符串转换错误

这是我的代码:

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

任何想法如何解决我的问题?谢谢!

4

1 回答 1

0

要将数组存储在数据库中,请使用 serialize() 或根据您的方便将其转换为 json。

$prepared->bindValue(':UsageType', serialize($usageType));
于 2016-05-16T05:52:39.253 回答