php - PDO ruining my JSON with " -
php - PDO ruining my JSON with " -
i'm storing json array in mysql text field using pdo. (encoded json_encode()
)
once data, if json_decode result null
.
it seems pdo replacing every " "
.
i have utilize pdo , json multiple times, first time got problem don't understand what's happening.
i'm on php 5.4.4
also, json sent ajax info
thanks if can help.
example of json in table :
{"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":""}
what see 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":""}
this inserted prepared query
code retrieve info :
$formdatasql = ' select * '.$this->prefix.' formalite_id = '.$this->proc_id.' '; $formdatareq = self::$db->prepare($formdatasql); $formdatareq->execute(); $formdata = $formdatareq->fetch(pdo::fetch_assoc);
then on json field :
$addrdata = json_decode(str_replace('"', '"', $champ['value']), true); // working not maintainable $addrdata = json_decode($champ['value'], true); // not working => null + json error = json_error_syntax
here simplified illustration of insert code :
foreach($savedata['personne_physique'] $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 personne_id = ".$this->id; $persphysreq = self::$db->prepare($persphyssql); $persphysreq->execute($values);
here how i'm connecting :
$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") );
this unusual :(
here illustration script based on info example:
<?php seek { $connection = new pdo('mysql:host=localhost;dbname=testing', user, pass); $connection->setattribute(pdo::attr_errmode, pdo::errmode_exception); }catch(pdoexception $e){ echo 'unable connect database' . "\n"; } $id = $argv[1]; seek { $data = $connection->prepare('select * `stackoverflow_26399231` 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"; } ?>
the database table looks this:
*************************** 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":""}
when run script in cli, back:
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] => )
how script differ yours? i'm not able reproduce issue.
update:
my update script next example:
try { $connection = new pdo('mysql:host=localhost;dbname=testing', user, pass); $connection->setattribute(pdo::attr_errmode, pdo::errmode_exception); }catch(pdoexception $e){ echo 'unable connect database' . "\n"; } $id = $argv[1]; $ary = array( 'foo' => 'bar', 'bar' => 'foo', 'a' => 'b', 'c' => 'd', 'e' => 'f' ); seek { $fields = 'data=:data'; $values = array('data' => json_encode($ary)); $update = $connection->prepare("update `stackoverflow_26399231` set $fields id = $id"); $update->execute($values); $connection = null; }catch(pdoexception $e){ echo 'error executing query: ' . $e->getmessage() . "\n"; }
using same script receive above changing pdo::fetch_obj pdo::fetch_assoc that's you're using, output:
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 )
so can still not reproduce issue you're having. there must different between 2 scripts.
php json pdo
Comments
Post a Comment