PHP save array made of dictionary items into MySQL table -


i’m trying save array in php table in mysql database. php receives array in json format. array made of list of dictionaries following:

{  {“first_name” : “sam”, “last_name”: “smith”, “email”: sam.smith@domain.com},  {“first_name” : “mike”, “last_name”: “detman”, “email”: mike.detman@domain.com},  {“first_name” : “linda”, “last_name”: “bennett”, “email”: linda.bennett@domain.com}} 

the question simplest way save database table corresponding columns keys “first_name”, “last_name”, , “email”?

many answers on web suggest using php function serialize(). method seems work saving 1 column table, may not working saving records multiple columns. (i might wrong…..)

others suggest using query “insert table (…) values(….)”. method seems working adding single record. can suggest way realize task? thank you!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ input far... here php code trying use loop (i'm dummy on php, please don't laugh if see nonsense....)

$receiveddata= json_decode($json_data);  $dbc=mysql_connect("mysql", "username", "passcode"); mysql_select_db("obtaineddata");  while ($linedata = mysql_fetch_array($receiveddata)) { $firstname = $linedata['first_name']; $lastname = $linedata['last_name']; $email= $linedata['email']; mysql_query("insert adata (first_name, last_name, email) values ($lastname, $firstname, $email)"); } 

is right way do? using right loop way or need other way? need serialize() data before doing loop?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ok, here latest version, still no data go database yet. however, based on time takes receive response, seems loop executed. , response right complete data... not knowing if there issues database setup or table settings?

<?php  $json_data=file_get_contents('php://input', true); $post_data = json_decode($json_data); $dbc=mysql_connect("mysql", "username", "passcode"); mysql_select_db("obtaineddata");  foreach($post_data $linedata) {$first_name=$linedata->first_name;  $last_name=$linedata->last_name;  $email=$linedata->email;  mysql_query("insert 'adata' ('first_name', 'last_name', 'email') values ('$first_name', '$last_name', '$email')");}  if (is_array($post_data)) $response = array("status" => "ok", "code" => 0, "original request" => $post_data); else $response = array("status" => "error", "code" => -1, "original_request" => $post_data); $processed = json_encode($response); echo $processed;  mysql_close($dbc);  ?> 

others suggest using query “insert table (…) values(….)”. method seems working adding single record.

no can work multiple records:

insert `table`   (`first_name`,`last_name`,`email`)   values    ('row 1 first name', 'row 1 last name', 'row 1 email'),   ('row 2 first name', 'row 2 last name', 'row 2 email') ... 

to construct query, need use json_decode on json string , loop on results row row construct tuple, can use array_map return tuple every record + implode concatenate tuples (if don't loops)

edit

(1)
json_decode here result in array of objects not array of associative arrays because json objects converted php stdclass objects (you can't $linedata['first_name'], $linedata->first_name allowed), if want access property in object in same way arrays $linedata['first_name'], have convert result associative array using json_decode($json_data, true)

(2)
loop o.k requires little modification:

  1. $receiveddata array n elements, correct way loop through array in php that: foreach($receiveddata $linedata)
    line using : while ($linedata = mysql_fetch_array($receiveddata)) used loop through resource (the 1 when use mysql_query select statement)
  2. always enclose table names , columns backticks insert `adata` (`first_name`, `last_name`... no conflicts happen if name reserved keyword
  3. mysql strings must enclosed in single or double quotes, correct query should like:
    mysql_query("insert `adata` (`first_name`, `last_name`, `email`) values ('$lastname', '$firstname', '$email'))"

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -