Advertisement
Riju18

no.95_dynamically_add_data_in_json_file_php

Apr 30th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. db.php:
  2. --------
  3. --------
  4. <?php
  5. $host = "localhost";
  6. $user = "root";
  7. $pass = "";
  8. $db = "test";
  9.  
  10.   $connect = mysqli_connect( $host, $user, $pass, $db );
  11.   if ( !$connect ) {
  12.     mysqli_error( $connect );
  13.   }
  14.  ?>
  15.  
  16. data_select.php:
  17. ---------------
  18. ---------------
  19. <?php
  20.   include 'db.php';
  21.  ?>
  22. <!DOCTYPE html>
  23. <html lang="en">
  24. <head>
  25.   <meta charset="UTF-8">
  26.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  27.   <meta http-equiv="X-UA-Compatible" content="ie=edge">
  28.   <title>select page</title>
  29. </head>
  30. <body>
  31.   <?php
  32.     $result = mysqli_query( $connect , $selectQuery );  //read operation from db
  33.     if ( $result ) {
  34.       if ( mysqli_num_rows($result) > 0 ) {
  35.         while ( $row = mysqli_fetch_assoc($result) ) {
  36.           $output[] = $row;
  37.         }
  38.       }
  39.       $finalOutput = json_encode($output);  //convert the data in json format from mysql;
  40.       $filename = "data.json";                     //the json file which is previously created;
  41.       file_put_contents( $filename, $finalOutput);  // store json data in json file;
  42.     }
  43.  
  44.    ?>
  45. </body>
  46. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement