Advertisement
Guest User

Untitled

a guest
May 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2. #This is for use on a local installation of XAMPP with the
  3. #the world database installed on the MySQL database engine
  4. #To use in the Streeter Annex you'll need to save this file in
  5. #c:\XAMPP\htdocs and start the XAMPP Apache & MySQL services
  6.  
  7. #To test at home, you'll need to XAMPP or MAMP or WAMP or similar_text
  8. # and then install the world database (linked from homework) - adjust
  9. # the $myUser, $myPass, $myDsn data below
  10.  
  11. //variables
  12. $myContinent = '';
  13. $myOutput = array();
  14.  
  15. $myUser = 'root';
  16. $myPass = '';
  17. $myDsn = 'mysql:host=localhost; dbname=worlddb';
  18.  
  19. //create connection to the database
  20. $myPDO = new PDO($myDsn, $myUser, $myPass);
  21.  
  22. //verify required data has been sent
  23. if(isset($_GET['selContinent'])) {
  24.     //create SELECT statement using a parameter
  25.     $myQuery = 'SELECT name, population, gnp, surfaceArea, lifeExpectancy FROM country WHERE continent LIKE :theName';
  26.    
  27.     //create the prepared SQL statement
  28.     $myPrepSql = $myPDO->prepare($myQuery);
  29.    
  30.     //execute the statement using the sent data as the parameter value
  31.     $myPrepSql->execute(array('theName' => $_GET['selContinent']));
  32.    
  33.     //retrieve all the records as an associative array
  34.     $myOutput = $myPrepSql->fetchAll(PDO::FETCH_ASSOC);
  35. }
  36.  
  37. //convert the associative array into JSON format
  38. echo json_encode($myOutput);
  39.  
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement