Guest User

Untitled

a guest
Jun 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <?php
  2. require_once 'mysqli_connection.php';
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7. //define table
  8. $tbl = "photos";
  9. $joinsTbl = "joins";
  10. $catsTbl = "categories";
  11.  
  12. //write query
  13. $query = "SELECT $tbl.photoID, $tbl.photoSRC, $tbl.photoCredit, $joinsTbl.categoryID,
  14. GROUP_CONCAT($catsTbl.photo_category SEPARATOR ',') AS categories
  15. FROM $tbl
  16. LEFT JOIN $joinsTbl
  17. INNER JOIN $catsTbl
  18. ON $joinsTbl.categoryID = $catsTbl.categoryID
  19. ON $tbl.photoID = $joinsTbl.photoID
  20. GROUP BY $tbl.photoID
  21. LIMIT 100";
  22. //prepare statement, execute, store_result
  23. if($displayStmt = $mysqli->prepare($query)){
  24. $displayStmt->execute();
  25. $displayStmt->store_result();
  26. $numrows = $displayStmt-> num_rows;
  27. //echo("<br>results: $numrows");
  28. //echo($displayStmt->error);
  29. }
  30.  
  31. $photosArray = [];
  32. //bind results
  33. $displayStmt->bind_result($photoID, $photoSRC, $photoCredit, $categoryID, $categories);
  34.  
  35. //fetch results
  36. while($displayStmt->fetch()){
  37. $photosArray[] = ['photoID'=>$photoID, 'photoSRC'=>$photoSRC, 'photoCredit'=>$photoCredit, 'categoryID'=>$categoryID, 'categories'=>$categories, ];
  38. }
  39.  
  40. echo(json_encode($photosArray));
  41.  
  42. ?>
Add Comment
Please, Sign In to add comment