Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // open mysql connection
- $host = "localhost";
- $username = "root";
- $password = "";
- $dbname = "marketing";
- $con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
- // use prepare statement for insert query
- // $st = mysqli_prepare($con, 'INSERT INTO emp(name, gender, designation) VALUES (?, ?, ?)');
- $st = mysqli_prepare($con, 'INSERT INTO U_ANSWERS(DRMMOBILE,QUECODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON)
- VALUES (?, ?, ?, ?, ?, ?, ?)');
- // bind variables to insert query params
- mysqli_stmt_bind_param($st, 'siiiisi', $drmobile, $quecode, $divcode, $brdcode, $prdcode, $answer, $createdon );
- // read json file
- $filename = 'employee.json';
- $json = file_get_contents($filename);
- //convert json object to php associative array
- $data = json_decode($json, true);
- if (is_array($values) || is_object($values))
- {
- // loop through the array
- foreach ($data as $row) {
- // get the employee details
- $drmobile = $row['DATA']['DRMMOBILE'];
- $divcode = $row['DATA']['DIVCODE'];
- $brdcode = $row['DATA']['BRDCODE'];
- $prdcode = $row['DATA']['PRDCODE'];
- $quecode = $row['ANSWERS']['QUECODE'];
- $answer= $row['ANSWERS']['ANSWER'];
- $createdon = $row['CREATEDON'];
- // execute insert query
- mysqli_stmt_execute($st);
- }
- }
- //close connection
- mysqli_close($con);
- ?>
- Sample Json Data
- {
- "DATA": {
- "DRMOBILE": "9652207972",
- "DIVCODE": "1",
- "BRDCODE": "1",
- "PRDCODE": "1"
- },
- "ANSWERS": [{
- "QUECODE": "1",
- "ANSWER": "EXCELLENT"
- },
- {
- "QUECODE": "2",
- "ANSWER": "EXCELLENT"
- },
- {
- "QUECODE": "3",
- "ANSWER": "EXCELLENT"
- }, {
- "QUECODE": "4",
- "ANSWER": "EXCELLENT"
- },
- {
- "QUECODE": "5",
- "ANSWER": "EXCELLENT"
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement