Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "172.19.0.2";
- $username = "root";
- $password = "asdf";
- $dbname = "json_test";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "select * from users";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) {
- // json_decode will give you a stdClass object after parsed
- // so you'll access by $user->firstname;
- $user = json_decode($row['data']);
- // stdClass Object ( [lastname] => Mungmana [firstname] => Mana [ชื่อ] => มานะ [นามสกุล] => มุ่งมานะ )
- echo 'Firstname: ' . $user->firstname . '<br>';
- echo 'Lastname: ' . $user->firstname . '<br>';
- echo 'ชื่อ: ' . $user->ชื่อ . '<br>';
- echo 'นามสกุล: ' . $user->นามสกุล . '<br>';
- echo '<br>';
- // OR access with array we'll put true in second argument of json_decode
- // we'll access using array e.g. $user['firstname'];
- $user = json_decode($row['data'], true);
- // Array ( [lastname] => Mungmana [firstname] => Mana [ชื่อ] => มานะ [นามสกุล] => มุ่งมานะ )
- echo 'Firstname: ' . $user['firstname'] . '<br>';
- echo 'Lastname: ' . $user['firstname'] . '<br>';
- echo 'ชื่อ: ' . $user['ชื่อ'] . '<br>';
- echo 'นามสกุล: ' . $user['นามสกุล'] . '<br>';
- echo '<br>';
- }
- } else {
- echo "0 results";
- }
- $conn->close();
- ?>
Add Comment
Please, Sign In to add comment