sql - Inset php json encoded array into database -
i need insert elements of php encoded array database , i'm stuck. have first used json encode to grab data database using sql query (which did successfully) need able opposite. if i'd appreciate it. it's second day of work , i'm not doing well. following code:
$usercoords_query = "select accuracy, longitude, latitude, timestamp userdetails userid =" . $userid; $usercoords_result = mysql_query($usercoords_query); if (mysql_num_rows($usercoords_result) == 0) { echo "there no users 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 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());
i agree chandresh_cool. should use 'true' option decode json encoded string array, otherwise return object.
moreover, file_get_contents() expects filename (with full or relative path). when try give json_encoded string, thinks file name , try open file, which, not exist, , throws error. try give existing filename , see solves problem.
p.s. know should comment, due insufficient points, cannot comment
Comments
Post a Comment