<?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 );
?>
|