Advertisement
radityakurnianto

getSpesificName.php

May 19th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2. /*declare variable as a result*/
  3. $response = array();
  4.  
  5. /*need db_connect.php to connect to database mahasiswa*/
  6. include('/db_connect.php');
  7. $db = new DB_CONNECT();
  8.  
  9. /*parameter send as get method in a tag name "nama_mhs"*/
  10. if(isset($_GET["nama_mhs"]))
  11. {
  12.     /*assigned varible $nama "nama_mhs" which is sent by get method*/
  13.     $nama = $_GET["nama_mhs"];
  14.     /*assigned $response with a select result from mysql_query*/
  15.     $result = mysql_query("SELECT nama_mhs FROM mahasiswa WHERE nama_mhs = '$nama'") or die (mysql_error());
  16.    
  17.     /*this if block check whether the $result from query is empty or not*/
  18.     if(!empty($result))
  19.     {   /*the second if block checked whether the result from $result is greater than one*/
  20.         if(mysql_num_rows($result) > 0)
  21.         {   /*declare a temporary variable collect result from $result query*/
  22.             $result = mysql_fetch_array($result);
  23.             /*declare anew array variable to collect each field from $result*/
  24.             $specificname = array();
  25.             $specificname["nama_mhs"] = $result["nama_mhs"];
  26.             /*success code*/
  27.             $response["success"] = 1;
  28.             /*create variable with TAG mahasiswa as an array*/
  29.             $respone["mahasiswa"] = array();
  30.             /*put the result from $specificname to TAG mahasiswa*/
  31.             array_push($response["mahasiswa"], $specificname);
  32.             /*send result as a json document*/
  33.             echo json_encode($response);
  34.         }
  35.         else
  36.         {   /*a block with no result*/
  37.             $response["success"] = 0;
  38.             $response["message"] = "Result is zero";
  39.             echo json_encode($response);
  40.         }
  41.     }
  42.     else
  43.     {   /*a block with no result*/
  44.         $response["success"] = 0;
  45.         $response["message"] = "No match name";
  46.         echo json_encode($response);
  47.     }
  48. }
  49. else
  50. {   /*a block with no parameter sent*/
  51.     $response["success"] = 0;
  52.     $response["message"] = "Require field is missing";
  53.     echo json_encode($response);
  54. }
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement