PHP Mysql更新转义撇号中的序列化数组

我有一个php
mysql更新序列化数据的问题.

我的序列化数据

a:1:{i:0;a:3:{s:5:"image";s:4:"5812";s:5:"title";s:14:"Day 1 : LOREM";s:4:"desc";s:416:"Lorem Ipsum is 'simply dummy' text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500's, when an unknown printer ";}}

问题出现在文本中,例如’撇号’为虚拟’.

Mysql Update语句

$conSave="update serialized_data set value='$str1' where id='{$_POST['key_id']}'";
$conSaveData = mysql_query($conSave);

如何在序列化数据中解决此问题?

MySQLi更新声明

$stmt =  $con->stmt_init();
$stmt->prepare("Update serialized_data set value=? WHERE id = ?");
$stmt->bind_param("ss",$a,$b);
$a = $str1;
$b = $_POST['key_id'];

$stmt->execute();

最佳答案 你的代码应该是这样的

try {
   $b = $_POST['key_id'];

   $stmt = new mysqli("example.com", "user", "password", "database");
   $stmt->prepare("Update serialized_data set value=? WHERE id = ?");
   $stmt->bind_param("si",$str1, $b);
   $stmt->execute();

} catch(Exception $e){
   echo $e->getMessage();
}

但是,我宁愿使用json字符串而不是序列化字符串.

像这样的东西

$data = array('key' => 'val');  // this is your original array before you serialize it
$a = json_encode($data);  //this will convert your array to a json string

然后当你从数据库中选择字符串时,你将使用json_decode($selectedString); //这会将你的json字符串转换为一个对象

点赞