Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. <?php
  2. // Parameters for connection
  3. $servername = "localhost"; //server
  4. $username = "root"; //username
  5. $password = "root"; //password
  6. $dbname = "employees"; //name of the table
  7. $response = array(); //array with data about 5 employess
  8.  
  9. // Creating connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11. // Checking connection
  12. if ($conn->connect_error) {
  13. echo "Connection failed: " . $conn->connect_error;
  14. }
  15.  
  16. // SQL request to get ID of 5 top paid employees
  17. $sql = "SELECT `emp_no`,`salary`, `to_date` FROM `salaries` ORDER BY `to_date` DESC, `salary` DESC LIMIT 5";
  18. $result = $conn->query($sql);
  19.  
  20. $counter = 0;
  21. //Checking if request was succesfull
  22. if ($result->num_rows > 0) {
  23.  
  24. //creating JSON file and opening writing link
  25. $fp = fopen('employees.json','w');
  26.  
  27. //dividing retrieved data into rows and going through trem
  28. while($row = $result->fetch_assoc()) {
  29. //the array that will hold data about 1 employee
  30. $posts = array();
  31.  
  32. //Requesting first and last names of employee
  33. $sql = "SELECT `first_name`, `last_name` FROM `employees` WHERE `emp_no` = {$row['emp_no']} LIMIT 1";
  34. $request = $conn->query($sql);
  35. //dividing data from DB and storing it
  36. $row1 = mysqli_fetch_row($request);
  37. $fname = $row1[0];
  38. $lname = $row1[1];
  39.  
  40. //Requesting tittle of the employee
  41. $sql = "SELECT `title` FROM `titles` WHERE `emp_no` = {$row['emp_no']} ORDER BY `to_date` LIMIT 1";
  42. $request = $conn->query($sql);
  43. //dividing data from DB and storing it
  44. $row1 = mysqli_fetch_row($request);
  45. $title = $row1[0];
  46.  
  47. //Requesting department name where employee works
  48. $sql = "SELECT `dept_name` FROM `departments`,`dept_emp` WHERE `dept_emp`.`emp_no` = {$row['emp_no']} AND `dept_emp`.`dept_no` = `departments`.`dept_no` ORDER BY `to_date` DESC LIMIT 1";
  49. $request = $conn->query($sql);
  50. //dividing data from DB and storing it
  51. $row1 = mysqli_fetch_row($request);
  52. $depart = $row1[0];
  53.  
  54.  
  55. //each piece of information from the requests goes from variables into the posts array
  56. $posts[] = array('Title'=> $title, 'First name'=> $fname, 'Last name'=> $lname, 'Title'=> $title, 'Salary'=> $row['salary'], 'Department'=> $depart);
  57.  
  58.  
  59. //the posts array goes into the response
  60. $counter++;
  61. $response["Employee {$counter}"] = $posts;
  62. }
  63. } else {
  64. echo "Request error" . mysql_error();
  65. }
  66.  
  67. //writing response array in to the JSON file
  68. fwrite($fp, json_encode($response));
  69. //closing writing link
  70. fclose($fp);
  71.  
  72. // Closing connection with DB
  73. $conn->close();
  74.  
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement