PDO破坏了我的JSON“

时间:2014-10-16 08:05:36

标签: php json pdo

我使用PDO将JSON数组存储在mysql TEXT字段中。 (以json_encode()编码)

获得数据后,如果我执行json_decode,结果为NULL

似乎PDO正在取代每一个"与"

我已经多次使用过PDO和JSON,但这是我第一次遇到这个问题,所以我不明白发生了什么。

我在PHP 5.4.4上

此外,JSON随AJAX一起发送,以供您参考

谢谢,如果你能提供帮助。

表格中的JSON示例:

{"type_voie":"BD","indice_repetition":"T","num_voie":"121","nom_voie":"NOM_RUE","infos_voie":"NOM_RUE2","distribution_speciale":"BP789","cp":"34000","ville":"MONTPELLIER","bureau_distributeur":""}

我真正看到的var_dump:

{"type_voie":"BD","indice_repetition":"T","num_voie":"121","nom_voie":"NOM_RUE","infos_voie":"NOM_RUE2","distribution_speciale":"BP789","cp":"34000","ville":"MONTPELLIER","bureau_distributeur":""}

使用准备好的查询

插入

检索数据的代码:

        $formDataSQL = '
            SELECT * FROM '.$this->prefix.'
            WHERE formalite_id = '.$this->proc_id.'
        ';
        $formDataReq = self::$db->prepare($formDataSQL);
        $formDataReq->execute();
        $formData = $formDataReq->fetch(PDO::FETCH_ASSOC);

然后在JSON字段上:

$addrData = json_decode(str_replace('"', '"', $champ['value']), true); // WORKING BUT NOT MAINTAINABLE
$addrData = json_decode($champ['value'], true); // NOT WORKING => NULL + JSON ERROR = JSON_ERROR_SYNTAX

以下是我的插入代码的简化示例:

        foreach($saveData['personne_physique'] as $field => $value){
            if(is_array($value)) $value = json_encode($value);
            $param = ':'.$field;
            $fields .= $field.'='.$param.', ';
            $values[$param] = $value;
        }
        $fields = trim($fields, ', ');

        $persPhysSQL = "UPDATE personne_physique SET $fields WHERE personne_id = ".$this->id;
        $persPhysReq = self::$db->prepare($persPhysSQL);
        $persPhysReq->execute($values);

以下是我的联系方式:

    $host = 'mysql:dbname='.BDD_NAME.';host='.BDD_HOST.';charset=utf8';
    $user = BDD_USER;
    $pass = BDD_PASS;
    $db = new PDO($host, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );

这很奇怪:(

1 个答案:

答案 0 :(得分:0)

以下是基于您的数据示例的示例脚本:

<?php
try {
        $Connection = new PDO('mysql:host=localhost;dbname=testing', USER, PASS);
        $Connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
        echo 'Unable to connect to database' . "\n";
}

$id = $argv[1];

try {
        $data = $Connection->prepare('SELECT * FROM `stackoverflow_26399231` WHERE ID = :id');
        $data->bindParam(':id', $id, PDO::PARAM_INT);
        $data->execute();
        while($row = $data->fetch(PDO::FETCH_OBJ)){
                print_r($row);
                print_r(json_decode($row->data));
        }
        $Connection = null;
}catch(PDOException $e){
        echo 'Error executing query: ' . $e->getMessage() . "\n";
}
?>

数据库表如下所示:

*************************** 1. row ***************************
  id: 1
data: {"type_voie":"BD","indice_repetition":"T","num_voie":"121","nom_voie":"NOM_RUE","infos_voie":"NOM_RUE2","distribution_speciale":"BP789","cp":"34000","ville":"MONTPELLIER","bureau_distributeur":""}

在CLI中运行脚本时,您会收到回复:

stdClass Object (
    [id] => 1
    [data] => {"type_voie":"BD","indice_repetition":"T","num_voie":"121","nom_voie":"NOM_RUE","infos_voie":"NOM_RUE2","distribution_speciale":"BP789","cp":"34000","ville":"MONTPELLIER","bureau_distributeur":""}
)

stdClass Object (
    [type_voie] => BD
    [indice_repetition] => T
    [num_voie] => 121
    [nom_voie] => NOM_RUE
    [infos_voie] => NOM_RUE2
    [distribution_speciale] => BP789
    [cp] => 34000
    [ville] => MONTPELLIER
    [bureau_distributeur] =>
)

我的脚本与您的脚本有何不同?我无法重现这个问题。

<强>更新

我的更新脚本遵循您的示例:

try {
        $Connection = new PDO('mysql:host=localhost;dbname=testing', USER, PASS);
        $Connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
        echo 'Unable to connect to database' . "\n";
}

$id = $argv[1];

$ary = array(
        'foo' => 'bar',
        'bar' => 'foo',
        'a' => 'b',
        'c' => 'd',
        'e' => 'f'
);

try {
        $fields = 'data=:data';
        $values = array('data' => json_encode($ary));
        $update = $Connection->prepare("UPDATE `stackoverflow_26399231` SET $fields WHERE ID = $id");
        $update->execute($values);
        $Connection = null;
}catch(PDOException $e){
        echo 'Error executing query: ' . $e->getMessage() . "\n";
}

使用相同的脚本如上所述接收但是从PDO :: FETCH_OBJ更改为PDO :: FETCH_ASSOC作为您正在使用的内容,我得到此输出:

Array
(
    [id] => 1
    [data] => {"foo":"bar","bar":"foo","a":"b","c":"d","e":"f"}
)

stdClass Object
(
    [foo] => bar
    [bar] => foo
    [a] => b
    [c] => d
    [e] => f
)

所以我仍然无法重现你所拥有的问题。两个脚本之间必须有不同的东西。