Skip to content

Instantly share code, notes, and snippets.

@jalbertbowden
Last active August 30, 2019 17:34
Show Gist options
  • Save jalbertbowden/42c3f642cb4134f23d1145b6dd3630ba to your computer and use it in GitHub Desktop.
Save jalbertbowden/42c3f642cb4134f23d1145b6dd3630ba to your computer and use it in GitHub Desktop.
moar ms sql fail by me
function passSQL(url, jsonSchedulesURIVar, htmlSelectAgenciesValue, queryFor){
var http = new XMLHttpRequest();
var data = new Object();
data.schedule = jsonSchedulesURIVar;
data.agency = htmlSelectAgenciesValue;
var jsonString= JSON.stringify(data);
http.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.response);
};
};
http.open('POST', url, true);
http.setRequestHeader('Content-Type', 'application/json');
http.send(jsonString);
};
<?php /** api endpoint - schedules **/
include("includes/sqlsrv.php");
header('Content-Type: application/json, charset=UTF-8');
$conn = sqlsrv_connect( $server_name, $connection_info); /* open the connection */
/* catch and display any connection error message */
if( $conn === false ) {
echo '<p>Unable to connect.</p>';
die( print_r( sqlsrv_errors(), true));
}
$request_payload = file_get_contents('php://input');
$payloads = json_decode($request_payload, true);
$scheduleVar = $payloads["schedule"];
$agencyVar = $payloads["agency"];
function escapeVars($str){
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
$str = htmlentities($str, ENT_QUOTES, 'UTF-8');
$str = (int)$str;
return $str;
};
$scheduleVar = escapeVars($scheduleVar);
$agencyVar = escapeVars($agencyVar);
$tsql_get_schedules = "'SELECT DISTINCT SchedNum, SchedTitle FROM RM3_Schedules WHERE FK_SCHEDULE_TYPE_ID = {$agencyVar} OR FK_AGENCY_LOCALITY_ID = {$scheduleVar} ORDER BY SchedTitle;'";
$stmt_tsql_get_schedules = sqlsrv_query($conn, $tsql_get_schedules); /* submit query to open connection */
if( !$stmt_tsql_get_schedules ) {
echo '<p>Error executing <code>get_schedules</code> query.</p>'; /* catch/display/query error message */
echo '<p>0 schedule is: '.$scheduleVar.', and 1 agency is: '.$agencyVar.'</p>';
echo '<p>0 schedule type is: '.gettype($scheduleVar).', and 1 agency type is: '.gettype($agencyVar).'</p>';
die( print_r( sqlsrv_errors(), true));
}
/* iterate through table rows populating the array */
do {
while ($row = sqlsrv_fetch_array($stmt_tsql_get_schedules, SQLSRV_FETCH_ASSOC)) {
$jsonSchedules[] = $row;
}
} while ( sqlsrv_next_result($stmt_tsql_get_schedules) );
$jsonEchoSchedules = json_encode($jsonSchedules);
echo $jsonEchoSchedules;
sqlsrv_free_stmt( $stmt); /* free up $stmt's resources */
sqlsrv_close( $conn); /* close $conn and release associated $conn resources */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment