Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:09:15+00:00 2026-06-04T21:09:15+00:00

I have some data in JSON format in a text file as below. I

  • 0

I have some data in JSON format in a text file as below. I need to insert this data into mysql using php but can’t do it.

{"address":"+92 334 6629424","service_center":"Test Sending Sms","id":3,"locked":0,"person":0,"protocol":0,"read":0,"reply_path_present":2,"seen":0,"error_code":0,"status":1,"date":1873326412,"thread_id":1,"type":-1}

My PHP file has the code like this.

<?php $source_file  = "SMS2012-05-21.txt"; $handle = fopen("SMS2012-05-21.txt", "r");
$col_names = implode(",", fgetcsv($handle)); // Getting comma separated list of col name

 $link = mysql_connect('localhost', 'root', '');
 mysql_select_db("messages");
 while (($data = fgetcsv($handle)) !== FALSE) {
 $values = "";
 foreach($data as $key => $value) {
     if ($key != 0) $values .= ", ";
     $values .= "'".mysql_escape_string($value)."'";
 }
 mysql_query('INSERT INTO messages ('.$col_names.') VALUES ('.$values.')');
 }
 ?>

I can’t find any result nor any error. Could any one please help me in this regard that where i am wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:09:16+00:00Added an answer on June 4, 2026 at 9:09 pm

    You should use json_decode function to manipulate json data.

    <?php
    
    
      $source_file  = "SMS2012-05-21.txt";
      $string = file_get_contents($source_file);
      $json = json_decode($string,true);
    
      //DB Conn Handling Stuff
    
      $cols = array(); $values = array();
    
      foreach($json as $key=>$value)
      {
          array_push($cols,'`' . $key . '`');
          if(is_string($value))
          {
             array_push($values,'\''.$value.'\'');
          }
          else
          {
             array_push ($values, $value);
          }
      }
    
      $col_name = implode(',',$cols);
      $col_value = implode(',',$values);
    
      $query = 'INSERT INTO messages('.$col_name.') VALUES ('.$col_value.')';
      mysql_query($query,$connection) or die(echo mysql_error());      
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data that I have to serialize to JSON. I'm using JSON.NET.
I have a little problem, I need to load some json data to my
I want to send some data in json format to php and do some
I am getting some data in JSON format via jQuery and then I need
I have a REST api which dumps some json data (user info, etc). Now
I have a website where an ajax call will get some Json data from
I have the following JSON data being returned, but for some reason, Javascript or
I have some data in a database that I need represented in an XML
I have a code similar to this: $.ajax({ success: function(data) { text = '';
I have some JSON data coming from a service. I don't know the exact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.