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"  
  }
 ]
    A PEM file is a text-based file that contains a certificate, a private key, and any associated certificates. The file DigiCertGlobalRootCA.crt.pem can be found from here.

    ~/public_html/demo/AJAX/StudentMySQL.php
    <?php
      header( "Access-Control-Allow-Origin: *" );
      header( "Content-Type: application/json; charset=UTF-8" );
    
      $username = "user_id";
      $password = "password";
      $database = "schema";
      $host     = "undcemmysql.mysql.database.azure.com";
    
      // Initializing MySQLi
      $conn     = mysqli_init( );
    
      // Creating an SSL connection
      mysqli_ssl_set( $conn, NULL, NULL, "DigiCertGlobalRootCA.crt.pem", NULL, NULL );
    
      // Opening a new connection to the MySQL server
      mysqli_real_connect( $conn, $host, $username, $password, $database, 3306 );
    
      // Connect to the database.
      if ( mysqli_connect_errno( ) )
        die( 'Failed to connect to MySQL: ' . mysqli_connect_error( ) );
    
      // Compose and execute the query.
      $query  = "SELECT name, city, country FROM student3";
      $result = mysqli_query( $conn, $query );
      $row    = mysqli_fetch_assoc( $result );
    
      // Compose the JSON object.
      $outp = "[";
      if ( mysqli_num_rows( $result ) > 0 )
        while( $row = mysqli_fetch_assoc( $result ) ) {
          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.
      mysqli_close( $conn );
    ?>


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.