Website Construction Summary (Cont.)

  1. Fetching Data by Using SQL
  2. The script below fetches dynamic data from a web server running SQL and returns a JSON object. JSON stands for JavaScript Object Notation. JSON, much like XML, is syntax for storing and exchanging text information. One example of JSON objects is shown on the right. The script is used in the file Student.html, where the method JSON.parse parses a string as JSON.

 [
  {
   "Name": "Alfreds Futterkiste",
   "City": "Berlin",
   "Country": "Germany"
  },
  {
   "Name": "Berglunds snabbköp",
   "City": "Luleå",
   "Country": "Sweden"
  },
  {
   "Name": "Island Trading",
   "City": "Cowes",
   "Country": "UK"
  },
  {
   "Name": "Ernst Handel",
   "City": "Graz",
   "Country": "Austria"
  },
  {
   "Name": "FISSA Fabrica Inter.",
   "City": "Madrid",
   "Country": "Spain"  
  }
 ]
    ~/public_html/demo/AJAX/StudentMySQL.php
    <?php
      header( "Access-Control-Allow-Origin: *" );
      header( "Content-Type: application/json; charset=UTF-8" );
    
      $username = "user.id@undcsmysql";
      $password = "password";
      $database = "schema";
      $host     = "undcsmysql.mysql.database.azure.com";
      $conn     = new mysqli( $host, $username, $password, $database );
    
      // Connect to the database.
      if ( $conn->connect_error )
        die( 'Could not connect: ' . $conn->connect_error );
    
      // Compose and execute the query.
      $query  = "SELECT name, city, country FROM student3";
      $result = $conn->query( $query );
      $outp = "[";
    
      // Print the results row by row.
      if ( $result->num_rows > 0 )
      while ( $row = $result->fetch_assoc( ) ) {
        if ( $outp != "[" ) { $outp .= ","; }
        $outp .= '{"Name":"'  . $row["name"]    . '",';
        $outp .= '"City":"'   . $row["city"]    . '",';
        $outp .= '"Country":"'. $row["country"] . '"}';
      }
      $outp .= "]";
    
      // Send the JSON to xmlhttp.responseText in Student.html.
      echo( $outp );
    
      // Close the connection.
      $conn->close( );
    ?>


Review: Website Construction
    Which statement is NOT true?

      Angular is syntax for storing and exchanging text information.
      CSS defines how to display HTML elements.
      HTML DOM is the standard for accessing HTML elements.
      JavaScript is a client-side language.
Result:        




      That’s your best friend and your worst enemy—your own brain.