0

I am new to database and php and not sure how to do the following. I am using wampserver which comes with mysql.

I am receiving a json data which i used json_decode to get a php 3-level array. Now how do i insert these data into a database and also display it in a table form?

I searched and there are quite a number of examples on insertion portion. But must I create a database with all the fields first before insertion? How to retrieve and display it after insertion?

Sample of the array to be inserted is as shown in another posting: insert from array has multi levels to database using php

4
  • I have no idea what your data look likes so at the first look the best choice would be to save json formatted string in database. Feel free to add more information in your question for better and more helpful answers/comments. ;)
    – Leri
    Jan 8, 2013 at 6:54
  • may i see your top secret json response and database table design and can you explain how you wanna store it in database because this doesn't make sense for how u wanna add it in database. Jan 8, 2013 at 6:54
  • @DipeshParmar I have added the link for the sample array above. Jan 8, 2013 at 7:12
  • I came here with a "PHP3... wtf" expression on my face.
    – Leigh
    Jan 8, 2013 at 8:03

2 Answers 2

2

I think you should serialize the data and store it in a single column. Use PHP serialize function.

$string = serialize($array);

Now store $string in a text field in DB. You dont need to create different fields for different variables. To retrieve the data from database, you should unserialize the string as:

$array = unserialize($string);

This approach will allow you to save as many variables in DB as you want without need to create separate fields for each variable.

3
  • Thanks, i have tried this before. The issue is the displaying portion, as there is a lot of data, it just shows a huge junk of data, which is very messy. Jan 8, 2013 at 7:18
  • You can access any particular variable after desirializing the data. Jan 8, 2013 at 7:19
  • Thanks, I understand, meaning when displaying, i retrieve deserialize and then display. Jan 8, 2013 at 7:21
0

I prefer to use an ORM such as Idiorm/Paris for accessing data in MySQL. It abstracts away the actual query language making it easier and less tedious. Basically you first create your tables in your database and then add/retrieve/remove data using a Model factory class.

<?php

// Our model extension class where MyData is the name of the table
Class MyData extends Model{}

// Create and save a new record
$data = Model::factory('MyData')->create();
$data->id = 1;
$data->somedata = 'important stuff';
$data->save();

// Retrieve a record
$data = Model::factory('MyData')->find_one(1);

?>
<table>
  <tr>
    <td><?php echo $data->somedata; ?></td>
  </tr>
</table>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.