Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. <?php
  2.  
  3.   $servername = "localhost";
  4.   $username = "root";
  5.   $password = "";
  6.   $dbname = "php_api_database";
  7.   $conn = mysqli_connect($servername,$username,$password,$dbname);
  8.  
  9.   if($conn) {
  10.     echo 'Succesfully connected to the database' . '<br>';
  11.   }
  12.   else {
  13.     die('Error.');
  14.   }
  15.  
  16.   $user_url = 'http://jsonplaceholder.typicode.com/users';
  17.   $user_json = file_get_contents($user_url);
  18.   $user_array = json_decode($user_json, true);
  19.  
  20.   // $sqlTable = "CREATE TABLE users (
  21.   // id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  22.   // name VARCHAR(30) NOT NULL,
  23.   // username VARCHAR(30) NOT NULL,
  24.   // email VARCHAR(50) NOT NULL,
  25.   // address VARCHAR(50) NOT NULL,
  26.   // phone VARCHAR(50) NOT NULL
  27.   // )";
  28.  
  29.   echo '<table id="table"> <tr> <th>Id</th> <th>Name</th> <th>Username</th> <th>Email</th> <th>Address</th> <th>Phone</th> </tr>';
  30.   if(!empty($user_array)){
  31.   foreach($user_array as $user){
  32.     echo '<tr><td>' . $user['id'] . '</td>';
  33.     echo '<td>' . $user['name'] . '</td>';
  34.     echo '<td>' . $user['username'] . '</td>';
  35.     echo '<td>' . $user['email'] . '</td>';
  36.     echo '<td>' . $user['address']['street'] . ', ' . $user['address']['suite'] . ', ' . $user['address']['city'] . ', ' . $user['address']['zipcode'] . '</td>';
  37.     echo '<td>' . $user['phone'] . '</td></tr>';
  38.  
  39.     $sql = "INSERT INTO users(name,username,email) VALUES ($user['name'];$user['username'];$user['email']);";
  40.  
  41.     // $sql = "INSERT INTO users ('name', 'username', 'email', 'address', 'phone')
  42.     // VALUES (
  43.     //   '".mysqli_real_escape_string($user['name']."',
  44.     //   '".mysqli_real_escape_string($user['username']."',
  45.     //   '".mysqli_real_escape_string($user['email']."',
  46.     //   '".mysqli_real_escape_string($user['address']['street']."',
  47.     //         '".mysqli_real_escape_string($user['address']['suite']."',
  48.     //               '".mysqli_real_escape_string($user['address']['city']."',
  49.     //                     '".mysqli_real_escape_string($user['address']['zipcode']."',
  50.     //   '".mysqli_real_escape_string($user['phone']."'
  51.     // )";
  52.  
  53.     // if (mysqli_query($conn, $sql)) {
  54.     //     echo "New record created successfully";
  55.     // } else {
  56.     //     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  57.     // }
  58.  
  59.     var_dump($sql);
  60.  
  61.   }}
  62.   echo '</table>';
  63.  
  64.   if (mysqli_query($conn, $sqlTable)) {
  65.       echo "Table users created successfully";
  66.   } else {
  67.       echo "Error creating table: " . mysqli_error($conn) . '<br>';
  68.   }
  69.   ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement