Created
August 30, 2019 18:08
-
-
Save jalbertbowden/da8c9d78c307b5af08ec553b81acf555 to your computer and use it in GitHub Desktop.
ms sql thinks my code sucks.
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_tsql_get_schedules); /* 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