簡體   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