Advertisement
rachmadi

profil.php

Jan 30th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. require("config.php");
  3.  
  4. if (!empty($_POST)) {
  5.  
  6. //initial query
  7.  
  8. $query = "SELECT nama, alamat, email, status FROM `tblpengguna` WHERE username=:user";
  9.            
  10. $query_params = array(
  11.         ':user' => $_POST['username']
  12.     );
  13.            
  14. //execute query
  15. try {
  16.     $stmt   = $db->prepare($query);
  17.     $result = $stmt->execute($query_params);
  18. }
  19. catch (PDOException $ex) {
  20.     $response["sukses"] = 0;
  21.     $response["pesan"] = "Database Error!";
  22.     die(json_encode($response));
  23. }
  24.  
  25. // Finally, we can retrieve all of the found rows into an array using fetchAll
  26. $rows = $stmt->fetchAll();
  27.  
  28.  
  29. if ($rows) {
  30.     $response["sukses"] = 1;
  31.     $response["pesan"] = "Profil tersedia!";
  32.     $response["profil"]   = array();
  33.    
  34.     foreach ($rows as $row) {
  35.         $post = array();
  36.         $post["nama"]  = $row["nama"];
  37.         $post["alamat"]    = $row["alamat"];
  38.         $post["email"] = $row["email"];
  39.         $post["status"] = $row["status"];
  40.        
  41.        
  42.         //update our repsonse JSON data
  43.         array_push($response["profil"], $post);
  44.     }
  45.    
  46.     // echoing JSON response
  47.     echo json_encode($response);
  48.    
  49.    
  50. } else {
  51.     $response["sukses"] = 0;
  52.     $response["profil"] = "Profil tidak tersedia!";
  53.     die(json_encode($response));
  54. }
  55. } else {
  56.     ?>
  57.         <h1>Lihat Profil</h1>
  58.         <form action="profil.php" method="post">
  59.             Nama Pengguna:<br />
  60.             <input type="text" name="username" placeholder="username" />
  61.             <br /><br />  
  62.             <input type="submit" value="Lihat Profil" />
  63.         </form>
  64.     <?php
  65.  
  66. }
  67.  
  68. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement