Last active
August 30, 2019 17:34
-
-
Save jalbertbowden/42c3f642cb4134f23d1145b6dd3630ba to your computer and use it in GitHub Desktop.
moar ms sql fail by me
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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