Guest User

Untitled

a guest
Oct 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "172.19.0.2";
  4. $username = "root";
  5. $password = "asdf";
  6. $dbname = "json_test";
  7.  
  8. // Create connection
  9. $conn = new mysqli($servername, $username, $password, $dbname);
  10. // Check connection
  11. if ($conn->connect_error) {
  12. die("Connection failed: " . $conn->connect_error);
  13. }
  14.  
  15. $sql = "select * from users";
  16. $result = $conn->query($sql);
  17.  
  18. if ($result->num_rows > 0) {
  19. // output data of each row
  20. while($row = $result->fetch_assoc()) {
  21.  
  22. // json_decode will give you a stdClass object after parsed
  23. // so you'll access by $user->firstname;
  24. $user = json_decode($row['data']);
  25. // stdClass Object ( [lastname] => Mungmana [firstname] => Mana [ชื่อ] => มานะ [นามสกุล] => มุ่งมานะ )
  26.  
  27. echo 'Firstname: ' . $user->firstname . '<br>';
  28. echo 'Lastname: ' . $user->firstname . '<br>';
  29.  
  30. echo 'ชื่อ: ' . $user->ชื่อ . '<br>';
  31. echo 'นามสกุล: ' . $user->นามสกุล . '<br>';
  32. echo '<br>';
  33.  
  34. // OR access with array we'll put true in second argument of json_decode
  35. // we'll access using array e.g. $user['firstname'];
  36.  
  37. $user = json_decode($row['data'], true);
  38. // Array ( [lastname] => Mungmana [firstname] => Mana [ชื่อ] => มานะ [นามสกุล] => มุ่งมานะ )
  39.  
  40. echo 'Firstname: ' . $user['firstname'] . '<br>';
  41. echo 'Lastname: ' . $user['firstname'] . '<br>';
  42.  
  43. echo 'ชื่อ: ' . $user['ชื่อ'] . '<br>';
  44. echo 'นามสกุล: ' . $user['นามสกุล'] . '<br>';
  45. echo '<br>';
  46. }
  47. } else {
  48. echo "0 results";
  49. }
  50.  
  51. $conn->close();
  52.  
  53. ?>
Add Comment
Please, Sign In to add comment