Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. <?php
  2. // open mysql connection
  3. $host = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "marketing";
  7. $con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
  8.  
  9. // use prepare statement for insert query
  10. // $st = mysqli_prepare($con, 'INSERT INTO emp(name, gender, designation) VALUES (?, ?, ?)');
  11. $st = mysqli_prepare($con, 'INSERT INTO U_ANSWERS(DRMMOBILE,QUECODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON)
  12. VALUES (?, ?, ?, ?, ?, ?, ?)');
  13.  
  14. // bind variables to insert query params
  15. mysqli_stmt_bind_param($st, 'siiiisi', $drmobile, $quecode, $divcode, $brdcode, $prdcode, $answer, $createdon );
  16.  
  17. // read json file
  18. $filename = 'employee.json';
  19. $json = file_get_contents($filename);
  20.  
  21. //convert json object to php associative array
  22. $data = json_decode($json, true);
  23. if (is_array($values) || is_object($values))
  24. {
  25.  
  26. // loop through the array
  27. foreach ($data as $row) {
  28. // get the employee details
  29. $drmobile = $row['DATA']['DRMMOBILE'];
  30. $divcode = $row['DATA']['DIVCODE'];
  31. $brdcode = $row['DATA']['BRDCODE'];
  32. $prdcode = $row['DATA']['PRDCODE'];
  33. $quecode = $row['ANSWERS']['QUECODE'];
  34. $answer= $row['ANSWERS']['ANSWER'];
  35. $createdon = $row['CREATEDON'];
  36.  
  37.  
  38. // execute insert query
  39. mysqli_stmt_execute($st);
  40. }
  41. }
  42.  
  43. //close connection
  44. mysqli_close($con);
  45. ?>
  46.  
  47. Sample Json Data
  48.  
  49. {
  50. "DATA": {
  51. "DRMOBILE": "9652207972",
  52. "DIVCODE": "1",
  53. "BRDCODE": "1",
  54. "PRDCODE": "1"
  55. },
  56. "ANSWERS": [{
  57. "QUECODE": "1",
  58. "ANSWER": "EXCELLENT"
  59. },
  60. {
  61. "QUECODE": "2",
  62. "ANSWER": "EXCELLENT"
  63. },
  64. {
  65. "QUECODE": "3",
  66. "ANSWER": "EXCELLENT"
  67. }, {
  68. "QUECODE": "4",
  69. "ANSWER": "EXCELLENT"
  70. },
  71. {
  72. "QUECODE": "5",
  73. "ANSWER": "EXCELLENT"
  74. }
  75. ]
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement