php – PDO截断字符串参数
发布时间:2020-12-13 21:48:25 所属栏目:PHP教程 来源:网络整理
导读:我有这个函数将数据插入表: public static function insertSistema($name,$description,$created_at,$updated_at,$img_file_name,$img_content_type,$img_file_size,$img_updated_at,$visible,$access_floors,$access_procedures,$access_datas,$access_his
我有这个函数将数据插入表:
public static function insertSistema($name,$description,$created_at,$updated_at,$img_file_name,$img_content_type,$img_file_size,$img_updated_at,$visible,$access_floors,$access_procedures,$access_datas,$access_histories,$access_incidences,$access_operations,$access_reports,$access_messagings) { $conector = new Conexion("localhost","xrem_prueba"); try { $con = $conector->Conectar(); $con->exec('SET CHARACTER SET utf8'); $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $consulta = $con->prepare("INSERT INTO systems (name,created_at,updated_at,img_file_name,img_content_type,img_file_size,img_updated_at,visible,description,access_floors,access_procedures,access_datas,access_histories,access_incidences,access_operations,access_reports,access_messagings ) VALUES (:name,:created_at,:updated_at,:img_file_name,:img_content_type,:img_file_size,:img_updated_at,:visible,:description,:access_floors,:access_procedures,:access_datas,:access_histories,:access_incidences,:access_operations,:access_reports,:access_messagings);"); $consulta->bindParam(':name',$name,PDO::PARAM_STR,57); $consulta->bindParam(':description',PDO::PARAM_STR); $consulta->bindParam(':created_at',PDO::PARAM_STR); $consulta->bindParam(':updated_at',PDO::PARAM_STR); $consulta->bindParam(':img_file_name',255); $consulta->bindParam(':img_content_type',255); $consulta->bindParam(':img_file_size',PDO::PARAM_INT); $consulta->bindParam(':img_updated_at',PDO::PARAM_INT); $consulta->bindParam(':visible',PDO::PARAM_INT,1); $consulta->bindParam(':access_floors',1); $consulta->bindParam(':access_procedures',1); $consulta->bindParam(':access_datas',1); $consulta->bindParam(':access_histories',1); $consulta->bindParam(':access_incidences',1); $consulta->bindParam(':access_operations',1); $consulta->bindParam(':access_reports',1); $consulta->bindParam(':access_messagings',$access_messagings,1); $datos = array('name'=>$name,'description'=>$description,'created_at'=>$created_at,'updated_at'=>$updated_at,'img_file_name'=>$img_file_name,'img_content_type'=>$img_content_type,'img_file_size'=>$img_file_size,'img_updated_at'=>$img_updated_at,'visible'=>$visible,'access_floors'=>$access_floors,'access_procedures'=>$access_procedures,'access_datas'=>$access_datas,'access_histories'=>$access_histories,'access_incidences'=>$access_incidences,'access_operations'=>$access_operations,'access_reports'=>$access_reports,'access_messagings'=>$access_messagings ); $consulta->execute($datos); $conector = null; $con = null; return $consulta; } catch (Exception $e) { $conector = null; $con = null; throw $e; } } 如你所见,我有这个$consulta-> bindParam(‘:name’,PDO :: PARAM_STR,57);第10行:bindParam选项,它支持截断57个字符的长度,但它会插入任何类型的字符,因此它不会截断. 解决方法
length参数不用于截断.它只应在参数来自存储过程时使用,然后用于表示长度 – 不设置它.您必须在绑定之前截断变量$name.
从PHP Manual:“数据类型的长度.要指示参数是存储过程的OUT参数,您必须明确设置长度.” (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |