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

时间:2015-07-12 22:42:49

标签: php mysql serialization

我有一个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更新声明

$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();

1 个答案:

答案 0 :(得分:2)

您的代码应该如下所示

language: node_js
node_js:
  - 0.12.2
addons:
  postgresql: "9.4"
before_install:
  - rvm install 2.2.2
install:
  # run whatever you have to do here. I have a Makefile that lets you install
  # all Node.js-related or Ruby-related dependencies as one step.
  - make npm
  - make bundler
before_script:
  # My Rails app lives in a subdirectory. I want to make sure that
  # my database is ready before I start running RSpec tests
  - psql -c 'create database test_db;' -U postgres
  # I use separate database.yml config for Travis CI
  - cp webapp/config/database.travis.yml webapp/config/database.yml
script:
  # `test` target executes `bundle exec rspec spec` and `npm run test`
  # in all appropriate subdirectories
  - make test

但是,我宁愿使用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