2

I need to insert elements of a php encoded array into a database and I'm completely stuck. I have first used json encode to to grab data from the database using an SQL query (which I did successfully) but I now need to be able to do the opposite. If anyone could help I'd greatly appreciate it. It's my second day of work and I'm not doing so well. The following is my code:

    $UserCoords_Query  = "Select Accuracy, Longitude, Latitude, Timestamp                            
        FROM UserDetails
          WHERE UserId =" . $UserID;
                  $UserCoords_result = mysql_query($UserCoords_Query);

if (mysql_num_rows($UserCoords_result) == 0) {
    echo "There are no users with an id of ". $UserID;
}

else {
            $EmptyArray=array();
    while ($row = mysql_fetch_array($UserCoords_result)) {
        $Accuracy=$row['Accuracy'];
        $Longitude= $row['Longitude'];
        $Latitude=$row['Latitude'];
        $Timestamp= $row['Timestamp'];

        $Queue= array('Accuracy:' => $Accuracy, 'Latitude' => $Latitude, 'Longitude' => $Longitude, 'Timestamp' => $Timestamp); 
        array_unshift($EmptyArray,$Queue);
}   

    $ObjectResponse = array('Coords' => $EmptyArray);
    echo json_encode($ObjectResponse);

    $Json_Encoded= json_encode($ObjectResponse);

   $Json_Decoded= json_decode($Json_Encoded, true);

    $Update_Query= "INSERT INTO UserDetails (UserId, Accuracy, Latitude, Longitude,         Timestamp)
    VALUES ('".$UserID."','".$Json_Decoded[0]        ['Accuracy']."','".$Json_Decoded[0]['Latitude']."',
    '".$Json_Decoded[0]['Longitude']."','".$Json_Decoded[0]['Timestamp']."')";

    mysql_query($Update_Query) or die(mysql_error());
3
  • what's the error you get? Commented May 8, 2013 at 16:04
  • you shouldn't be inserting json text into a db, unless you plan to NEVER access any individual sub-data from the json text with sql. json's a transport format, not a storage format. Commented May 8, 2013 at 16:05
  • @ Marc B Well I'd need to explode the string to get the elements and add them to the DB. I thought that was what the json decode function does? Commented May 8, 2013 at 16:24

2 Answers 2

1

I agree with chandresh_cool. you should use 'true' option to decode the json encoded string as array, otherwise it will return an object.

moreover, file_get_contents() expects a filename (with full or relative path). When you try to give a json_encoded string, it thinks that its the file name and it will try to open it as a file, which, obviously does not exist, and thus throws an error. Try to give an existing filename and see that solves the problem.

P.s. I know this should be a comment, but due to insufficient points, I cannot comment

Sign up to request clarification or add additional context in comments.

14 Comments

@ dhavald I have edited the code to include the true option and removed the file get contents function. However, I am still receiving the error:Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/roseanne/public_html/display_Coords.php on line 26
No because the query involves using variables from the decoded array but I have some mistake involving that decode.
@ dhavald If it helps, I was using the following link as a guideline. But it didn't work. stackoverflow.com/questions/14736880/…
@ dhavald That produces this??: array(1) { ["Coords"]=> array(2) { [0]=> array(4) { ["Accuracy"]=> string(2) "56" ["Latitude"]=> string(2) "78" ["Longitude"]=> string(2) "89" ["Timestamp"]=> string(3) "909" } [1]=> array(4) { ["Accuracy"]=> string(2) "56" ["Latitude"]=> string(2) "78" ["Longitude"]=> string(2) "89" ["Timestamp"]=> string(3) "909" } } }
@ dhavald Do you know how I could access these elements? In my query I'm using $Json_Decoded[0]['Accuracy'] etc., but that obviously isn't working
|
0

You need to set second parameter of json_encode as true to return array

$Json_Decoded = json_decode($Json_Encoded, true);

2 Comments

Thanks, I tried that. However, I'm still getting an error of "Warning: file_get_contents({"Coords":[{"Accuracy:":"56","Latitude":"78","Longitude":"89","Timestamp":"909"}]}): failed to open stream: No such file or directory in /home/roseanne/public_html/display_Coords.php on line 48"
@ chandresh_cool this is the only error I get. Line 48 is this line: $Json_Data = file_get_contents($Json_Encoded);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.