Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying for some time now to insert data from a JSON array into mysql database using php but what ever I try it is not working.

my array looks like so...

Array  
(  
    [] => -4.0533  
    [bert] => 2
    [earnie] => 0.25  
    [bigbird] => 0.25  
    [grouch] => 1.25  
)

I am trying to insert this data into mysql database that has a table named "useramounts" the table contains 2 columns. (username,amount) so that each row contains a username and the associated amount

this is probably very simple for you guys but I have never attempted this before. I have tried to google a solution but to no avail. Can anyone help me?

share|improve this question

closed as too broad by CRABOLO, Kermit, jww, Ed Cottrell, dcastro Feb 13 '14 at 18:46

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What have you tried so far? Perhaps reading up on PHP/MySQL and following a tutorial is a better place to start. If you have a specific question during the process, feel free to ask it here. – Joel Feb 13 '14 at 18:13
    
thanks joel for your polite response but i give up now so dont worry about it. – hd-pixel Feb 13 '14 at 18:15
    
possible duplicate of Insert JSON array in mysql db from a php file – jww Feb 13 '14 at 18:37
    
@noloader: The linked question does have an answer that is encouraging bad practice (it's creating an insert per loop). – Aquillo Feb 13 '14 at 18:43
up vote 2 down vote accepted

What have you tried?

Try this approach:

  • Convert JSON to PHP array (json_decode())
  • Loop through the array, get the key and value for each entry (foreach(){}, array_keys())
  • Create a single string with an insert and add VALUES() for each row
  • Execute the query after the loop

    $keys = array_keys($array);              // get the value of keys
    $rows = array();                         // create a temporary storage for rows
    foreach($keys as $key) {                 // loop through
        $value = $array[$key];               // get corresponding value
        $rows[] = "('" . $key . "', '" . $value . "')";
                                             // add a row to the temporary storage 
    }
    $values = implode(",", $rows);           // 'glue' your rows into a query
    $query = "INSERT INTO ... VALUES " . $values;
                                             // write the rest of your query
    ...                                      // execute query
    

As soon as you find a concrete question, feel free to open another post.

share|improve this answer

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