Student.html, where the method JSON.parse parses a string as JSON.
|
|
DigiCertGlobalRootCA.crt.pem can be found from here.
|
|
<?php
header( "Access-Control-Allow-Origin: *" );
header( "Content-Type: application/json; charset=UTF-8" );
$username = "your_id";
$database = "your_db";
$password = "your-pw";
$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 ( $conn->connect_errno )
die( 'Failed to connect to MySQL: ' . $conn->connect_error );
// Compose and execute the query.
$query = "SELECT name, city, country FROM student3";
$result = $conn->query( $query );
$row = $result->fetch_assoc( );
// Compose the JSON object.
$outp = "[";
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 database connection.
$conn->close( );
?>
|
| That’s your best friend and your worst enemy—your own brain. |