繁体   English   中英

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

[英]PHP Mysql Update Serialize Array in escape apostrophe

我有一个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); // this will convert your json string into an object json_decode($selectedString); // this will convert your json string into an object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM