Skip to content

Instantly share code, notes, and snippets.

@vvikramjhu
Last active December 19, 2015 00:31
Show Gist options
  • Save vvikramjhu/3ba385c9af93af90f38c to your computer and use it in GitHub Desktop.
Save vvikramjhu/3ba385c9af93af90f38c to your computer and use it in GitHub Desktop.
// Question -- I have a complicated string ( line 24). I apply a json_decode on it and it works fine ( result box). I then insert
// ( see insert statement below) this String in mysql(5.6). I retrieve this string from db and echo it.
// It looks fine ( result box line 5). I try json_decode on it.
// This is the same string as line 24. It returns A NULL. Why ?
<?php
$username = "root";
$password = "";
$database_name = "test";
$host = "localhost";
$port = "3306";
if($host == ""){
$host = "127.0.0.1";
}
if($port == ""){
$port = "3306";
}
$port = intval($port);
$mysqli = new mysqli($host, $username, $password, $database_name, $port);
if ($mysqli->connect_errno) {
$error_string = sprintf("Connect failed: %s\n", $mysqli->connect_error);
}
$key = "";
$value = " ";
$staticData = array();
$sql = "select demo_variable, demo_hard_coded_value from test.dashboard";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$stmt->bind_result($key, $value);
$x = '["$47,171", "1300, 1877, 2500, 2577, 2000, 2100, 3000, 2700, 3631, 2471, 2700, 3631, 2471", "<i class=\"fa fa-arrow-circle-up\"></i>&nbsp;45%", "110,150,300,130,400,240,220,310,220,300, 270, 210","<i class=\"fa fa-shopping-cart\"></i>&nbsp;2447", "110,150,300,130,400,240,220,310,220,300, 270, 210"]';
echo '<pre>'.var_dump(json_decode($x)).'</pre>';
while($stmt->fetch()){
echo '<pre>'.var_dump($value).'</pre>';
echo '<pre>'.var_dump(json_decode($value)).'</pre>';
$staticData[$key] = $value;
//if ("No Error" !== ($msg = json_last_error_msg ())) { die ($msg); }
//if ($msg = json_last_error_msg()) { die ($msg); }
}
$stmt->free_result();
$stmt->close();
insert into dashboard(demo_variable, demo_hard_coded_value) values('sparks','["$47,171","1300,1877,2500,2577,2000,2100,3000,2700,3631,2471,2700,3631,2471","<i class=\"fa fa-arrow-circle-up\"></i>&nbsp;45%","110,150,300,130,400,240,220,310,220,300,270,210","<i class=\"fa fa-shopping-cart\"></i>&nbsp;2447","110,150,300,130,400,240,220,310,220,300,270,210"]');
// line 25
array(6) { [0]=> string(7) "$47,171" [1]=> string(76) "1300, 1877, 2500, 2577, 2000, 2100, 3000, 2700, 3631, 2471, 2700, 3631, 2471" [2]=> string(46) " 45%" [3]=> string(49) "110,150,300,130,400,240,220,310,220,300, 270, 210" [4]=> string(45) " 2447" [5]=> string(49) "110,150,300,130,400,240,220,310,220,300, 270, 210" }
// line 28 and 29
string(295) "["$47,171", "1300, 1877, 2500, 2577, 2000, 2100, 3000, 2700, 3631, 2471, 2700, 3631, 2471", " 45%", "110,150,300,130,400,240,220,310,220,300, 270, 210"," 2447", "110,150,300,130,400,240,220,310,220,300, 270, 210"]"
NULL
array(6) {
[0]=>
string(7) "$47,171"
[1]=>
string(76) "1300, 1877, 2500, 2577, 2000, 2100, 3000, 2700, 3631, 2471, 2700, 3631, 2471"
[2]=>
string(46) "<i class="fa fa-arrow-circle-up"></i>&nbsp;45%"
[3]=>
string(49) "110,150,300,130,400,240,220,310,220,300, 270, 210"
[4]=>
string(45) "<i class="fa fa-shopping-cart"></i>&nbsp;2447"
[5]=>
string(49) "110,150,300,130,400,240,220,310,220,300, 270, 210"
}
<pre></pre>string(275) "["$47,171","1300,1877,2500,2577,2000,2100,3000,2700,3631,2471,2700,3631,2471","<i class="fa fa-arrow-circle-up"></i>&nbsp;45%","110,150,300,130,400,240,220,310,220,300,270,210","<i class="fa fa-shopping-cart"></i>&nbsp;2447","110,150,300,130,400,240,220,310,220,300,270,210"]"
NULL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment