【发布时间】:2016-09-11 20:22:07
【问题描述】:
我需要在我的一个表字段中插入一个数组。所以我定义了一个函数来将输入转换为这样的数组:
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
任何想法如何解决我的问题?谢谢!
【问题讨论】:
-
为什么不将值序列化并插入
-
你将如何将数组存储到数据库字段?您必须对其进行序列化,转换为 JSON 或您喜欢的任何字符串格式。
-
感谢@NirojAdhikary 的快速响应,我不知道在数据库中存储数组,序列化解决了我的问题。
标签: php mysql arrays pdo bindvalue