我需要在其中一个表中插入作为数组的字段。所以我定义了一个函数来将输入转换为数组,如下所示:
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
知道如何解决我的问题吗?谢谢
若要将数组存储在数据库中,请根据需要使用serialize()或将其转换为json。
$prepared->bindValue(':UsageType', serialize($usageType));