RegalChivas

MYSQL to JSON getuser.php

Jan 24th, 2021 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2.     //Koneksi ke DB
  3.     $host = "localhost";
  4.     $user = "root";
  5.     $password = "";
  6.     $database = "ucup";
  7.  
  8.     $koneksi = mysqli_connect($host,$user,$password,$database);
  9.  
  10.     if($koneksi->connect_error){die("Koneksi gagal");}
  11.     //Akhir Koneksi ke DB
  12.    
  13.     $hasil  = mysqli_query($koneksi,"select * from user");
  14.     if(mysqli_num_rows($hasil) > 0 )
  15.     {
  16.         $response = array();
  17.         while($x = mysqli_fetch_array($hasil))
  18.         {
  19.             $h['username'] = $x["username"];
  20.             $h['password'] = $x["password"];
  21.             array_push($response, $h);
  22.         }
  23.         header('Content-type: application/json');
  24.         echo json_encode($response);
  25.     }
  26.     else
  27.     {
  28.         echo "Data tidak tersedia";
  29.     }
  30. ?>
Add Comment
Please, Sign In to add comment