Advertisement
Guest User

php get api

a guest
Apr 13th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. <?php
  2.     $host="localhost"; //replace with database hostname
  3.     $username="username"; //replace with database username
  4.     $password="password"; //replace with database password
  5.     $db_name="db_name"; //replace with database name
  6.     $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
  7.     mysql_select_db("$db_name")or die("cannot select DB");
  8.    
  9.     if($_SERVER["REQUEST_METHOD"] == "POST") {
  10.         $matric = $_POST['matric'];
  11.         $sql = "select * from students where `matric` = '$matric'";
  12.     } else {
  13.         $sql = "select * from students";
  14.     }
  15.     $result = mysql_query($sql);
  16.     $json = array();
  17.     if(mysql_num_rows($result)){
  18.         while($row=mysql_fetch_assoc($result)){
  19.             $json['stu_info'][]=$row;
  20.         }
  21.     }
  22.     mysql_close($con);
  23.     echo json_encode($json);
  24. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement