/** * Fetch the $_POST vars and format into JSON * to POST to GoToWebinar * Return value is a webinar key * Also, write vals into webinars db */ function createWebinar() { global $globalCurlOptions, $db; $startDateAry = explode("/", $_POST['startDate']); $startTimeAry = explode(":", $_POST['startTime']); $startTS = mktime($startTimeAry[0], $startTimeAry[1], 0, $startDateAry[0], $startDateAry[1], $startDateAry[2]); $duration = $_POST['duration'] * 60 * 60; $endTS = $startTS + $duration; $description = $_POST['presenter'] . "\r\n" . $_POST['description']; $webinarDetails = array("subject" => $_POST['title'], "description" => $description, "times" => array(array("startTime" => date("c", $startTS), "endTime" => date("c", $endTS))), "timeZone" => $_POST['timezone']); $tokenAry = getAuthToken("gtw"); $accessToken = $tokenAry['access_token']; $organizerKey = $tokenAry['organizer_key']; $webinarInfo = json_encode($webinarDetails); $ch = curl_init(); $url = "https://api.citrixonline.com:443/G2W/rest/organizers/" . $organizerKey . "/webinars"; $headers = array("Authorization: " . $accessToken, "Accept: application/json", "Content-Type: application/json; charset=UTF-8", "Content-Length: " . strlen($webinarInfo)); curl_setopt_array($ch, $globalCurlOptions); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $webinarInfo); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); $mtgInfo = json_decode($result, TRUE); $webinarKey = $mtgInfo['webinarKey']; // Add to DB $q = "INSERT INTO webinars SET\n title = '" . mysqli_real_escape_string($db, $_POST['title']) . "',\n presenter = '" . mysqli_real_escape_string($db, $_POST['presenter']) . "',\n description = '" . mysqli_real_escape_string($db, $_POST['description']) . "',\n startTime = '" . date("Y-m-d H:i:00", $startTS) . "',\n endTime = '" . date("Y-m-d H:i:00", $endTS) . "',\n timezone = '" . $_POST['timezone'] . "',\n campaignID = '" . $_POST['campaignID'] . "',\n webinarKey = '{$webinarKey}'"; if (!mysqli_query($db, $q)) { printf("Error: %s\n", mysqli_sqlstate($db)); } mysqli_close($db); echo "<p>Webinar key {$webinarKey} has been created</p>"; }
function dump_properties($mysqli) { printf("\nClass variables:\n"); $variables = array_keys(get_class_vars(get_class($mysqli))); sort($variables); foreach ($variables as $k => $var) { printf("%s = '%s'\n", $var, var_export(@$mysqli->{$var}, true)); } printf("\nObject variables:\n"); $variables = array_keys(get_object_vars($mysqli)); foreach ($variables as $k => $var) { printf("%s = '%s'\n", $var, var_export(@$mysqli->{$var}, true)); } printf("\nMagic, magic properties:\n"); assert(@mysqli_affected_rows($mysqli) === @$mysqli->affected_rows); printf("mysqli->affected_rows = '%s'/%s ('%s'/%s)\n", @$mysqli->affected_rows, gettype(@$mysqli->affected_rows), @mysqli_affected_rows($mysqli), gettype(@mysqli_affected_rows($mysqli))); assert(@mysqli_get_client_info() === @$mysqli->client_info); printf("mysqli->client_info = '%s'/%s ('%s'/%s)\n", @$mysqli->client_info, gettype(@$mysqli->client_info), @mysqli_get_client_info(), gettype(@mysqli_get_client_info())); assert(@mysqli_get_client_version() === @$mysqli->client_version); printf("mysqli->client_version = '%s'/%s ('%s'/%s)\n", @$mysqli->client_version, gettype(@$mysqli->client_version), @mysqli_get_client_version(), gettype(@mysqli_get_client_version())); assert(@mysqli_errno($mysqli) === @$mysqli->errno); printf("mysqli->errno = '%s'/%s ('%s'/%s)\n", @$mysqli->errno, gettype(@$mysqli->errno), @mysqli_errno($mysqli), gettype(@mysqli_errno($mysqli))); assert(@mysqli_error($mysqli) === @$mysqli->error); printf("mysqli->error = '%s'/%s ('%s'/%s)\n", @$mysqli->error, gettype(@$mysqli->error), @mysqli_error($mysqli), gettype(@mysqli_error($mysqli))); assert(@mysqli_field_count($mysqli) === @$mysqli->field_count); printf("mysqli->field_count = '%s'/%s ('%s'/%s)\n", @$mysqli->field_count, gettype(@$mysqli->field_count), @mysqli_field_count($mysqli), gettype(@mysqli_field_count($mysqli))); assert(@mysqli_insert_id($mysqli) === @$mysqli->insert_id); printf("mysqli->insert_id = '%s'/%s ('%s'/%s)\n", @$mysqli->insert_id, gettype(@$mysqli->insert_id), @mysqli_insert_id($mysqli), gettype(@mysqli_insert_id($mysqli))); assert(@mysqli_sqlstate($mysqli) === @$mysqli->sqlstate); printf("mysqli->sqlstate = '%s'/%s ('%s'/%s)\n", @$mysqli->sqlstate, gettype(@$mysqli->sqlstate), @mysqli_sqlstate($mysqli), gettype(@mysqli_sqlstate($mysqli))); assert(@mysqli_get_host_info($mysqli) === @$mysqli->host_info); printf("mysqli->host_info = '%s'/%s ('%s'/%s)\n", @$mysqli->host_info, gettype(@$mysqli->host_info), @mysqli_get_host_info($mysqli), gettype(@mysqli_get_host_info($mysqli))); /* note that the data types are different */ assert(@mysqli_info($mysqli) == @$mysqli->info); printf("mysqli->info = '%s'/%s ('%s'/%s)\n", @$mysqli->info, gettype(@$mysqli->info), @mysqli_info($mysqli), gettype(@mysqli_info($mysqli))); assert(@mysqli_thread_id($mysqli) > @$mysqli->thread_id); assert(gettype(@$mysqli->thread_id) == gettype(@mysqli_thread_id($mysqli))); printf("mysqli->thread_id = '%s'/%s ('%s'/%s)\n", @$mysqli->thread_id, gettype(@$mysqli->thread_id), @mysqli_thread_id($mysqli), gettype(@mysqli_thread_id($mysqli))); assert(@mysqli_get_proto_info($mysqli) === @$mysqli->protocol_version); printf("mysqli->protocol_version = '%s'/%s ('%s'/%s)\n", @$mysqli->protocol_version, gettype(@$mysqli->protocol_version), @mysqli_get_proto_info($mysqli), gettype(@mysqli_get_proto_info($mysqli))); assert(@mysqli_get_server_info($mysqli) === @$mysqli->server_info); printf("mysqli->server_info = '%s'/%s ('%s'/%s)\n", @$mysqli->server_info, gettype(@$mysqli->server_info), @mysqli_get_server_info($mysqli), gettype(@mysqli_get_server_info($mysqli))); assert(@mysqli_get_server_version($mysqli) === @$mysqli->server_version); printf("mysqli->server_version = '%s'/%s ('%s'/%s)\n", @$mysqli->server_version, gettype(@$mysqli->server_version), @mysqli_get_server_version($mysqli), gettype(@mysqli_get_server_version($mysqli))); assert(@mysqli_warning_count($mysqli) === @$mysqli->warning_count); printf("mysqli->warning_count = '%s'/%s ('%s'/%s)\n", @$mysqli->warning_count, gettype(@$mysqli->warning_count), @mysqli_warning_count($mysqli), gettype(@mysqli_warning_count($mysqli))); printf("\nAccess to undefined properties:\n"); printf("mysqli->unknown = '%s'\n", @$mysqli->unknown); @($mysqli->unknown = 13); printf("setting mysqli->unknown, @mysqli_unknown = '%s'\n", @$mysqli->unknown); $unknown = 'friday'; @($mysqli->unknown = $unknown); printf("setting mysqli->unknown, @mysqli_unknown = '%s'\n", @$mysqli->unknown); printf("\nAccess hidden properties for MYSLQI_STATUS_INITIALIZED (TODO documentation):\n"); assert(@mysqli_connect_error() === @$mysqli->connect_error); printf("mysqli->connect_error = '%s'/%s ('%s'/%s)\n", @$mysqli->connect_error, gettype(@$mysqli->connect_error), @mysqli_connect_error(), gettype(@mysqli_connect_error())); assert(@mysqli_connect_errno() === @$mysqli->connect_errno); printf("mysqli->connect_errno = '%s'/%s ('%s'/%s)\n", @$mysqli->connect_errno, gettype(@$mysqli->connect_errno), @mysqli_connect_errno(), gettype(@mysqli_connect_errno())); }
function _session_post_finish($link, $logData) { require 'config_files.php'; // finish the session specified in the request $finishTime = time(); $finishTimeText = date('Y-m-d H:i:s', $finishTime); // TODO: Need to check to see if this has been closed, already. // if so, return an error, otherwise, update the record. $thisParam = 'sessionId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $sessionId = $logData[$thisParam]; } } else { $badParam[$thisParam] = "Missing"; } if (empty($badParam)) { // close the task 0 record for this session $query = 'UPDATE ' . $DB_TABLE_SESSION_LOG . ' SET endTime = "' . $finishTimeText . '" WHERE sessionId = ' . $sessionId . ' AND taskId = 0'; $result = mysqli_query($link, $query); // $response['debug']['query'] = $query; // $response['debug']['result'] = $result; if ($result) { $rData = array(); $rData['sessionId'] = $sessionId; $rData['finishTime'] = $finishTimeText; $response['data'] = $rData; } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error finishing session_log entry'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['update1'] = $localErr; } } else { // a bad parameter was passed $localErr = ''; $localErr['message'] = 'Bad parameter in request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $logData; // $localErr['globals'] = $GLOBALS; $errData['validation'] = $localErr; } if (!empty($errData)) { $response['error'] = $errData; } return $response; }
function _gratuity_get_studyId($link, $logData) { require 'config_files.php'; // check the parameters $thisParam = 'studyId'; if (!is_numeric($logData)) { $badParam[$thisParam] = "Not a number"; } else { $studyId = $logData; } if (empty($badParam)) { // read conifguration for this study and condition $query = 'SELECT * FROM ' . $DB_TABLE_GRATUITY_LOG . ' WHERE studyId = ' . $studyId; $result = mysqli_query($link, $query); $idx = 0; if (mysqli_num_rows($result) > 0) { while ($thisRecord = mysqli_fetch_assoc($result)) { // remove the recordSeq field unset($thisRecord['recordSeq']); $response['data'][$idx] = array_merge($thisRecord); foreach ($response['data'][$idx] as $k => $v) { // set "null" strings to null values if ($v == 'NULL') { $response['data'][$k] = NULL; } } $idx += 1; } } if ($idx == 0) { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'No gratuity records found'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['queryData'] = $localErr; $response['error'] = $errData; } } else { // bad parameter in request data $errData['message'] = 'studyId is missing from the query string.'; $errData['paramError'] = $badParam; $errData['request'] = $logData; // $errData['globals'] = $GLOBALS; $response['error'] = $errData; } return $response; }
function _gratuity_post_gratuity($link, $logData) { require 'config_files.php'; // create a new gratuity_log record if (!empty($logData)) { // TODO: Chceck fields // add server-generated fields to insert query $dbColList = 'recordSeq'; $dbValList = '0'; // add the client-provided fields foreach ($logData as $dbCol => $dbVal) { isset($dbColList) ? $dbColList .= ', ' : ($dbColList = ''); isset($dbValList) ? $dbValList .= ', ' : ($dbValList = ''); $dbColList .= $dbCol; if (empty($dbVal) && strlen($dbVal) == 0) { $dbValList .= 'NULL'; } else { $escapedString = str_replace("'", "''", $dbVal); $dbValList .= '\'' . $escapedString . '\''; } } $queryString = 'INSERT INTO ' . $DB_TABLE_GRATUITY_LOG . ' (' . $dbColList . ') VALUES (' . $dbValList . ')'; $qResult = mysqli_query($link, $queryString); if (!$qResult) { // SQL ERROR $localErr = ''; $localErr['sqlQuery'] = $queryString; $localErr['result'] = 'Error creating session_config record'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['insert1'] = $localErr; $response['error'] = $errData; } else { // finish start response buffer $response['data'] = $logData; } } return $response; }
public function get_sql_array($sql) { $arr = array(); //Это должно помочь восстанавливать соединения если они разорвались $this->reconnect(); $result = mysqli_query($this->sql_interface, $sql); //echo $sql; if (!$result) { $error_no = mysqli_errno($this->sql_interface); $error_text = mysqli_error($this->sql_interface); $sqlstate_text = mysqli_sqlstate($this->sql_interface); $this->mastdie(0, "сдох при чтении :( - " . $sqlstate_text . " Error: {$error_no}:" . $error_text . " SQL:" . $sql); } try { while ($row = $result->fetch_assoc()) { $arr[] = $row; } mysqli_free_result($result); } catch (Exception $e) { $this->echo_log($e); } return $arr; }
public function install(&$err_list, $script_file_name = '') { $err_list = array(); if ($script_file_name == '') { $script_file_name = Kohana::$config->load('install.install_sql_script_name'); } Helper::set_installation_status(INSTALLING); $a = Helper::get_install_settings(); $link = @mysqli_connect($a['db_path'], $a['db_login'], $a['db_password'], $a['db_name']); if (!$link) { //throw new Exception($message, $code, $previous) $err_list[0]['error'] = __('Error connecting to MySQL') . ' №' . mysqli_connect_errno() . ': ' . mysqli_connect_error() . '.'; return false; } else { // mysqli_query($link,'SET NAMES utf8'); $i = 0; $a = file($script_file_name); $a = Arr::map('trim', $a); foreach ($a as $n => $l) { if (substr($l, 0, 2) == '--' or $l == '') { unset($a[$n]); } } $a = explode(";\n", implode("\n", $a)); //unset($a[count($a)-1]); foreach ($a as $n => $q) { if ($q) { if (!mysqli_query($link, $q)) { $err_list[$i]['errno'] = mysqli_errno($link); $err_list[$i]['sqlstate'] = mysqli_sqlstate($link); $err_list[$i]['error'] = mysqli_error($link); $i++; } } } $q = 'INSERT INTO `roles` (`id`, `name`, `description`) VALUES' . '(1, \'login\', \'Login privileges, granted after account confirmation\'),' . '(2, \'admin\', \'Administrative user, has access to everything.\');'; if (!mysqli_query($link, $q)) { $err_list[$i]['errno'] = mysqli_errno($link); $err_list[$i]['sqlstate'] = mysqli_sqlstate($link); $err_list[$i]['error'] = mysqli_error($link); $i++; } $install = Helper::get_install_settings(); $q = 'INSERT INTO users (id,username, password) VALUES (1,"' . $install['installer_login'] . '","' . Auth::instance()->hash($install['installer_password']) . '")'; if (!mysqli_query($link, $q)) { $err_list[$i]['errno'] = mysqli_errno($link); $err_list[$i]['sqlstate'] = mysqli_sqlstate($link); $err_list[$i]['error'] = mysqli_error($link); $i++; } $q = 'INSERT INTO `roles_users` (`user_id`, `role_id`) VALUES (1, 1),(1, 2);'; if (!mysqli_query($link, $q)) { $err_list[$i]['errno'] = mysqli_errno($link); $err_list[$i]['sqlstate'] = mysqli_sqlstate($link); $err_list[$i]['error'] = mysqli_error($link); $i++; } // @mysqli_multi_query($link,$c); // // while (mysqli_more_results($link)) { // $result = mysqli_next_result($link); // $discard = mysqli_store_result($link); // //if (!$result){ // if (mysqli_errno($link)){ // $err_list[$i]['errno']=mysqli_errno($link); // $err_list[$i]['sqlstate']=mysqli_sqlstate($link); // $err_list[$i]['error']=mysqli_error($link); // $i++; // } // } if ($i == 0) { Helper::set_installation_status(INSTALLED); } return $i == 0 ? true : false; } }
$todate = $_POST['to']; $roomtype = $_POST['rtype']; $adults = $_POST['adults']; $children = $_POST['children']; $roomno = $_POST['rno']; if ($con) { $qdb = "SELECT * FROM register WHERE roomno = '{$roomno}';"; $ds = "INSERT INTO register(name, email, phone, fromdate, todate, rtype, ano, cno, roomno) VALUES('{$name}', '{$email}', '{$phone}', '{$fromdate}', '{$todate}', '{$roomtype}', '{$adults}', '{$children}', '{$roomno}');"; $check = mysqli_query($con, $qdb); $find = mysqli_num_rows($check); if ($find == 0) { $query = mysqli_query($con, $ds); if ($query) { echo "<p><center> Thank You !</center></p>"; } else { echo "Error !" . mysqli_sqlstate($con); } } else { header("Location:nobook.html"); } } else { echo " Couldn't get to DB"; } } ?> <!DOCTYPE html> <html> <link rel="stylesheet" type="text/css" href="book.css"> <head><title> Booking successful </title></head>
/** * Renvoi le code de la dernière erreur * */ public function errorCode() { if (is_object($this->id)) { $code = mysqli_sqlstate($this->id); return empty($code) ? null : $code; } }
function _study_get_allids($link, $logData) { require 'config_files.php'; //test request type if ($logData['studyId'] == '*') { // return the specified configuration $query = 'SELECT DISTINCT studyId FROM ' . $DB_TABLE_STUDY_CONFIG; $result = mysqli_query($link, $query); $recordIndex = 0; $response['data']['count'] = mysqli_num_rows($result); if ($response['data']['count'] > 0) { while ($thisRecord = mysqli_fetch_assoc($result)) { $response['data']['studyIds'][$recordIndex] = $thisRecord['studyId']; $recordIndex = $recordIndex + 1; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Reading study config returned ' . mysqli_num_rows($result) . ' records'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query'] = $localErr; $response['error'] = $errData; } } else { //get the details for a specific study // check the parameters $thisParam = 'studyId'; if (empty($logData[$thisParam]) || !is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Missing or not a number"; } else { $studyId = $logData[$thisParam]; } if (empty($badParam)) { // return the specified configuration $query = 'SELECT DISTINCT taskId, conditionId FROM ' . $DB_TABLE_STUDY_CONFIG . ' WHERE studyId = ' . $studyId; $result = mysqli_query($link, $query); $lastTaskId = -1; $conditionIdCount = 0; if (mysqli_num_rows($result) > 0) { $response['data']['studyId'] = $studyId; $response['data']['conditionCount'] = 0; $response['data']['conditionsBalanced'] = true; $response['data']['count'] = 0; while ($thisRecord = mysqli_fetch_assoc($result)) { if ($lastTaskId != $thisRecord['taskId']) { // set up for a new task $lastTaskId = $thisRecord['taskId']; $conditionIdCount = 0; $response['data']['count'] = $response['data']['count'] + 1; } $response['data']['tasks'][$thisRecord['taskId']][$conditionIdCount] = $thisRecord['conditionId']; $conditionIdCount = $conditionIdCount + 1; } // test task and condition symmetry: each task should have the same conditions // look for a difference. They shoud all be the same length $lastConditionCount = -1; foreach ($response['data']['tasks'] as $thisTask) { if ($lastConditionCount == -1) { $lastConditionCount = count($thisTask); } else { if ($lastConditionCount != count($thisTask)) { $response['data']['conditionsBalanced'] = false; } } } if ($response['data']['conditionsBalanced'] == true) { $response['data']['conditionCount'] = $lastConditionCount; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Reading study config returned ' . mysqli_num_rows($result) . ' records'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query'] = $localErr; $response['error'] = $errData; } } else { // bad parameter in request data $errData['message'] = 'Bad parameter in request.'; $errData['paramError'] = $badParam; $errData['request'] = $logData; // $errData['globals'] = $GLOBALS; $response['error'] = $errData; } } return $response; }
function log_get_allids($link, $logData) { require 'config_files.php'; $response['debug']['logData'] = $logData; if ($logData['studyId'] == '*') { // return the specified configuration $query = 'SELECT DISTINCT s.studyId FROM ' . $DB_TABLE_SESSION_CONFIG . ' AS s JOIN ' . $DB_TABLE_TRANSITION_LOG . ' AS l' . ' WHERE l.sessionId = s.sessionId ORDER BY l.sessionId'; $result = mysqli_query($link, $query); $recordIndex = 0; $response['data']['count'] = mysqli_num_rows($result); if ($response['data']['count'] > 0) { while ($thisRecord = mysqli_fetch_assoc($result)) { $response['data']['studyIds'][$recordIndex] = $thisRecord['studyId']; $recordIndex = $recordIndex + 1; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Reading study config returned ' . mysqli_num_rows($result) . ' records'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query'] = $localErr; $response['error'] = $errData; } } else { //get the details for a specific study // check the parameters $thisParam = 'studyId'; if (empty($logData[$thisParam]) || !is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Missing or not a number"; } else { $studyId = $logData[$thisParam]; } if (empty($badParam)) { // return the specified configuration $query = 'SELECT DISTINCT s.sessionId, l.taskId FROM ' . $DB_TABLE_SESSION_CONFIG . ' AS s JOIN ' . $DB_TABLE_TRANSITION_LOG . ' AS l' . ' WHERE l.sessionId = s.sessionId AND s.studyId = ' . $studyId . ' ORDER BY l.sessionId, l.taskId'; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) > 0) { $response['data']['studyId'] = $studyId; $response['data']['count'] = 0; while ($thisRecord = mysqli_fetch_assoc($result)) { $response['data']['sessionIds'][$thisRecord['sessionId']][] = $thisRecord['taskId']; } $response['data']['count'] = count($response['data']['sessionIds']); } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Reading study config returned ' . mysqli_num_rows($result) . ' records'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query'] = $localErr; $response['error'] = $errData; } } else { // bad parameter in request data $errData['message'] = 'Bad parameter in request.'; $errData['paramError'] = $badParam; $errData['request'] = $logData; // $errData['globals'] = $GLOBALS; $response['error'] = $errData; } } return $response; }
$sql = "INSERT INTO users(firstname,lastname,email) VALUES('{$firstname}','{$lastname}','{$email}')"; // If result is anything other than true then there's an error if (mysqli_query($link, $sql)) { // Create a flash message showing the user that it worked flash_message('alert alert-success', 'You are registered.'); //redirect to the home page header("Location: index.php"); } else { // we shouldn't get here unless there's an error // Error 1062 is a specific error that means "this email already exists in the system, emails must be unique" // Since we're "catching" this error we can display a custom error message and continue on our way if (mysqli_errno($link) == 1062) { flash_message('alert alert-danger', 'That email already exists.'); } else { // This is for all other errors. It is less friendly. It just prints the error message that the server returns" printf("Error: %s\n", mysqli_sqlstate($link)); } } // It always a good idea to close the link to the server/database when you are done // In big projects this can improve your site's performance mysqli_close($link); } ?> <!DOCTYPE html> <html lang="en"> <?php include_once 'head.php'; ?> <body class="homepage">
public function set_driver_error($state = null, $mode = PDO::ERRMODE_SILENT, $func = '') { if ($state === null) { $state = mysqli_sqlstate($this->link); } $this->set_error(mysqli_errno($this->link), mysqli_error($this->link), $state, $mode, $func); }
// format and send output $fnResponse = $jsonpTag . '(' . json_encode($thisRecord) . ')'; } else { // no callback param name so return an error // this line only works on PHP > 5.4.0, which not everyone seems to have. // http_response_code(500); // this works on PHP > 4.3 (or so) $response['data'] = $thisRecord; } } } else { if (!headers_sent()) { header('content-type: application/json'); header('X-PHP-Response-Code: 200', true, 200); } $errData['message'] = mysqli_error($link); $errData['sqlQuery'] = $query_string; $errData['sqlError'] = mysqli_sqlstate($link); $response['error'] = $errData; } if (!headers_sent()) { header('content-type: application/json'); header('X-PHP-Response-Code: 200', true, 200); } if (!empty($fnResponse)) { print $fnResponse; } else { print json_encode($response); } mysqli_close($link); }
die("Error opening database: " . mysqli_connect_error()); } echo "Creating a new table ...", PHP_EOL; // drop table if there is one already, then create a new one mysqli_query($con, "DROP TABLE IF EXISTS Places"); mysqli_query($con, "CREATE TABLE Places(id INT NOT NULL AUTO_INCREMENT\r\n , PRIMARY KEY(id)\r\n , City VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , District VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Type VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Name VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Speciality VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Address VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Phone1 VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Phone2 VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci\r\n , Fax VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci )") or die(mysql_error()); echo "Saving places in database ...", PHP_EOL; $count = 0; foreach ($sheetData as $rec) { $count += 1; if ($count == 1) { continue; } // skip data row $City = $rec['A']; $District = $rec['B']; $Type = $rec['C']; $Name = $rec['D']; $Speciality = $rec['E']; $Address = $rec['F']; $Phone1 = $rec['G']; $Phone2 = $rec['H']; $Fax = $rec['I']; $sql = "INSERT INTO Places\r\n (City, District, Type, Name, Speciality, Address, Phone1, Phone2, Fax)\r\n VALUES\r\n ('{$City}', '{$District}', '{$Type}', '{$Name}', '{$Speciality}', '{$Address}', '{$Phone1}', '{$Phone2}', '{$Fax}')"; if (!mysqli_query($con, $sql)) { die("Could not save place #{$count}: " . mysqli_sqlstate()); } echo "."; } echo PHP_EOL; echo "Done! Total places: {$count}", PHP_EOL;
function _session_post_start($link, $logData) { require 'config_files.php'; // start a new task and return a start response // get the number of conditions to pick from $numConditions = 0; $thisParam = 'studyId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $studyId = $logData[$thisParam]; } } else { $badParam[$thisParam] = "Missing"; } if (empty($badParam)) { $query = "SELECT COUNT(studyId) AS conditionCount FROM " . $DB_TABLE_STUDY_CONFIG . " WHERE studyId = " . $studyId . " AND taskId = 1"; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 1) { if ($thisRecord = mysqli_fetch_assoc($result)) { $numConditions = $thisRecord['conditionCount']; } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading condition count record'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query1data'] = $localErr; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading condition count'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query1'] = $localErr; } if ($numConditions > 0) { // query study config for a random condition // **note sessionId will probably come from elsewhere, so // ** we'll just get a timestamp for now to keep it unique $thisCondtion = round(mt_rand(1, $numConditions), 0, PHP_ROUND_HALF_UP); $sessionId = time(); $startTimeText = date('Y-m-d H:i:s', $sessionId); $thisStudySession = NULL; $thisTask = 0; // the first task a session starts with is task 0 // create a new session_log record $query = 'INSERT INTO ' . $DB_TABLE_SESSION_LOG . ' (recordSeq, studyId, sessionId, taskId, conditionId, startTime, endTime) VALUES ' . '(NULL, \'' . $logData['studyId'] . '\', \'' . $sessionId . '\', \'' . $thisTask . '\', \'' . $thisCondtion . '\', \'' . $startTimeText . '\', NULL)'; $result = mysqli_query($link, $query); if (!$result) { // SQL ERROR $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error creating new session_log record'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['insert1'] = $localErr; } else { // format start response buffer $sessionBuff['studyId'] = $logData['studyId']; $sessionBuff['sessionId'] = $sessionId; $sessionBuff['conditionId'] = $thisCondtion; $sessionBuff['startTime'] = $startTimeText; $response['data'] = $sessionBuff; } } } else { // a bad parameter was passed $localErr = ''; $localErr['message'] = 'Bad parameter in request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $logData; // $localErr['globals'] = $GLOBALS; $errData['validation'] = $localErr; } if (!empty($errData)) { $response['error'] = $errData; } return $response; }
$dbhost = 'dbosch-pi-2.student.iastate.edu'; $dbuser = '******'; $dbpassword = '******'; //get variables $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; //connect to database $con = new mysqli($dbhost, $dbuser, $dbpassword); //check connection if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit; } //check if username exists $checkName = mysqli_query($con, "SELECT * FROM `db309SD`.`members` WHERE `username` = '" . $username . "'"); $rows = mysqli_num_rows($checkName); if ($rows == 0) { //add new account $add = mysqli_query($con, "INSERT INTO `db309SD`.`members` (`username`, `password`, `email`) VALUES ('" . $username . "', '" . $password . "', '" . $email . "');"); if ($add) { printf("Account created!"); exit; } else { printf("Error : %s\n", mysqli_sqlstate($con)); exit; } } else { printf("Username already exists!"); exit; }
function log_get_log($link, $postData) { require 'config_files.php'; // query the database for the requested info // check the parameters // we should have either a study ID (which returns all tasks and sessions for a study) // or a session and task ID $thisParam = 'studyId'; $studyId = 0; if (array_key_exists($thisParam, $postData)) { $studyId = trim($postData[$thisParam]); if (!is_numeric($studyId)) { $badParam[$thisParam] = "Not a number"; } } else { // if no study, then check for a session and task ID $thisParam = 'sessionId'; $sessionId = 0; if (array_key_exists($thisParam, $postData)) { $sessionId = trim($postData[$thisParam]); if (!is_numeric($sessionId)) { $badParam[$thisParam] = "Not a number"; } } else { $badParam[$thisParam] = "Missing"; } $thisParam = 'taskId'; $taskId = 0; if (array_key_exists($thisParam, $postData)) { $taskId = trim($postData[$thisParam]); if (!is_numeric($taskId)) { $badParam[$thisParam] = "Not a number"; } } else { // Task ID == 0 == All tasks in the session $taskId = 0; } } //+ if (empty($badParam)) { // no parameter errors, so get task configuration record // first get the open records $response['debug']['studyId'] = $studyId; $response['debug']['sessionId'] = $sessionId; $response['debug']['taskId'] = $taskId; if ($taskId > 0) { $query = 'SELECT * FROM ' . $DB_TABLE_TRANSITION_LOG . ' WHERE taskId = ' . $taskId . ' AND sessionId = ' . $sessionId . ' ORDER BY serverTimestamp ;'; } else { if ($sessionId > 0) { // get all tasks for this session $query = 'SELECT * FROM ' . $DB_TABLE_TRANSITION_LOG . ' WHERE sessionId = ' . $sessionId . ' ORDER BY serverTimestamp ;'; } else { if ($studyId > 0) { $query = 'SELECT s.studyId, s.sessionId, l.serverTimestamp, l.clientTimestamp, l.sessionId, l.taskId, l.conditionId, ' . 'l.fromUrl, l.toUrl, l.linkClass, l.linkId, l.linkTag ' . 'FROM session_config AS s ' . 'JOIN log_transition AS l ' . 'WHERE l.sessionId = s.sessionId AND s.studyId = ' . $studyId . ' ORDER BY l.sessionId, l.taskId, serverTimestamp'; } } } $response['debug']['query'] = $query; $result = mysqli_query($link, $query); if ($result) { $openLogRecords = array(); $openLogRecordCount = 0; while ($thisRecord = mysqli_fetch_assoc($result)) { unset($thisRecord['recordSeq']); if ($taskId > 0) { $openLogRecords[$openLogRecordCount] = array_merge($thisRecord); $openLogRecordCount = $openLogRecordCount + 1; } else { $thisTask = $thisRecord['taskId']; if (empty($openLogRecords[$thisTask])) { $openLogRecords[$thisTask] = array(); } array_push($openLogRecords[$thisTask], array_merge($thisRecord)); } } $response['data'] = $openLogRecords; } else { // query error $respData['sqlQuery'] = $query; $respData['result'] = 'Error logging data to OPEN log'; $respData['sqlError'] = mysqli_sqlstate($link); $respData['message'] = mysqli_error($link); $response['error'] = $respData; } } else { // bad or missing parameter $localErr = ''; $localErr['message'] = 'Bad parameter in log request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $postData; // $errData['globals'] = $GLOBALS; $errData['validation'] = $localErr; } //- if (!empty($errData)) { $response['error'] = $errData; } return $response; }
function _session_get_log($link, $logData) { require 'config_files.php'; // check the parameters $thisParam = 'sessionId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $sessionId = $logData[$thisParam]; } } else { $badParam[$thisParam] = "Missing"; } $thisParam = 'taskId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $taskId = $logData[$thisParam]; } } else { $badParam[$thisParam] = "Missing"; } if (empty($badParam)) { // read conifguration for this study and condition $query = 'SELECT * FROM ' . $DB_TABLE_SESSION_LOG . ' WHERE sessionId = ' . $logData['sessionId'] . ' AND taskId = ' . $logData['taskId']; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 1) { //TODO: Add support for taskId = * if ($thisRecord = mysqli_fetch_assoc($result)) { // remove the recordSeq field unset($thisRecord['recordSeq']); $response['data'] = array_merge($thisRecord); foreach ($response['data'] as $k => $v) { // set "null" strings to null values if ($v == 'NULL') { $response['data'][$k] = NULL; } } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading config query'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['queryData'] = $localErr; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Record matching request could not be found.'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['queryData'] = $localErr; } } else { // a bad parameter was passed $localErr = ''; $localErr['message'] = 'Bad parameter in request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $logData; // $localErr['globals'] = $GLOBALS; $errData['validation'] = $localErr; } if (!empty($errData)) { $response['error'] = $errData; } return $response; }
function _task_get_config($link, $logData) { require 'config_files.php'; // check the parameters $thisParam = 'sessionId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $sessionId = $logData[$thisParam]; } } else { $badParam[$thisParam] = "Missing"; } $thisParam = 'taskId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $taskId = $logData[$thisParam]; } } else { // a missing taskId is OK. // we'll just all tasks // TODO: one of these days $taskId = 0; } if (empty($badParam)) { // no parameter errors, so get task configuration record if ($taskId == -1) { // -1 ==> get the latest task config for this session $query = 'SELECT * FROM ' . $DB_TABLE_SESSION_CONFIG . ' WHERE sessionId = ' . $sessionId . ' ORDER BY taskId DESC LIMIT 1'; } else { if ($taskId > 0) { // get the config record by session and task $query = 'SELECT * FROM ' . $DB_TABLE_SESSION_CONFIG . ' WHERE sessionId = ' . $sessionId . ' AND taskId = ' . $taskId; } else { // task == 0 is not supported yet // bad parameter $localErr = ''; $localErr['message'] = 'Bad parameter in config request.'; $localErr['paramError']['taskId'] = "Cannot be 0"; $localErr['request'] = $logData; // $errData['globals'] = $GLOBALS; $errData['validation'] = $localErr; } } if (!empty($query)) { $result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 1) { if ($thisRecord = mysqli_fetch_assoc($result)) { // remove the recordSeq field unset($thisRecord['recordSeq']); $response['data'] = array_merge($thisRecord); foreach ($response['data'] as $k => $v) { // set "null" strings to null values if ($v == 'NULL') { $response['data'][$k] = NULL; } } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading config query'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['queryData'] = $localErr; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Reading study config returned ' . mysqli_num_rows($result) . ' records'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query'] = $localErr; } } } else { //bad parameter $localErr = ''; $localErr['message'] = 'Bad parameter in start request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $logData; // $errData['globals'] = $GLOBALS; $errData['validation'] = $localErr; } if (!empty($errData)) { $response['error'] = $errData; } return $response; }
public function set_driver_error($state = null, $mode = EhrlichAndreas_Pdo_Abstract::ERRMODE_SILENT, $func = '') { if ($state === null) { $state = mysqli_sqlstate($this->link); } $this->set_error(mysqli_errno($this->link), mysqli_error($this->link), $state, $mode, $func); }
<?php require_once "connect.inc"; $tmp = NULL; $link = NULL; if (!is_null($tmp = @mysqli_sqlstate())) { printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } if (!is_null($tmp = @mysqli_sqlstate($link))) { printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } require 'table.inc'; var_dump(@mysqli_sqlstate($link, "foo")); var_dump(mysqli_sqlstate($link)); mysqli_query($link, "SELECT unknown_column FROM test"); var_dump(mysqli_sqlstate($link)); mysqli_free_result(mysqli_query($link, "SELECT id FROM test")); var_dump(mysqli_sqlstate($link)); mysqli_close($link); var_dump(mysqli_sqlstate($link)); print "done!";
/** * Checks SQL operation errors, rollbacks any ongoing transactions and logs all the data * @param string $sql * The faulty SQL query string * @throws Exception * Returns a message telling there's been an error while trying to perform an operation in the database */ private function _checkErrors($sql = "") { $errno = mysqli_errno($this->dbLink); $error = mysqli_error($this->dbLink); $sqlstate = mysqli_sqlstate($this->dbLink); if ($sqlstate) { //Rollback the uncommited changes just in case $this->_failedTransaction(); error_log("Database error #" . $errno . " (" . $sqlstate . "): " . $error . "\n", 3, self::DB_LOG_FILE); if ($sql != "") { error_log("Caused by the following SQL command: " . $sql . "\n", 3, self::DB_LOG_FILE); } throw new Exception("Database operation error\n"); } else { return; } }
public function isUpdated() { return mysqli_sqlstate($this->conn) == 00 ? true : false; }
assert(mysqli_get_client_version() === $mysqli->client_version); printf("mysqli->client_version = '%s'/%s ('%s'/%s)\n", $mysqli->client_version, gettype($mysqli->client_version), mysqli_get_client_version(), gettype(mysqli_get_client_version())); assert(mysqli_errno($link) === $mysqli->errno); printf("mysqli->errno = '%s'/%s ('%s'/%s)\n", $mysqli->errno, gettype($mysqli->errno), mysqli_errno($link), gettype(mysqli_errno($link))); assert(mysqli_error($link) === $mysqli->error); printf("mysqli->error = '%s'/%s ('%s'/%s)\n", $mysqli->error, gettype($mysqli->error), mysqli_error($link), gettype(mysqli_error($link))); if (version_compare(PHP_VERSION, '5.3.99', '>')) { assert(mysqli_error_list($link) === $mysqli->error_list); assert(is_array($mysqli->error_list)); } assert(mysqli_field_count($link) === $mysqli->field_count); printf("mysqli->field_count = '%s'/%s ('%s'/%s)\n", $mysqli->field_count, gettype($mysqli->field_count), mysqli_field_count($link), gettype(mysqli_field_count($link))); assert(mysqli_insert_id($link) === $mysqli->insert_id); printf("mysqli->insert_id = '%s'/%s ('%s'/%s)\n", $mysqli->insert_id, gettype($mysqli->insert_id), mysqli_insert_id($link), gettype(mysqli_insert_id($link))); assert(mysqli_sqlstate($link) === $mysqli->sqlstate); printf("mysqli->sqlstate = '%s'/%s ('%s'/%s)\n", $mysqli->sqlstate, gettype($mysqli->sqlstate), mysqli_sqlstate($link), gettype(mysqli_sqlstate($link))); assert(soundex(mysqli_stat($link)) == soundex($mysqli->stat)); printf("mysqli->stat = '%s'/%s ('%s'/%s)\n", $mysqli->stat, gettype($mysqli->stat), mysqli_stat($link), gettype(mysqli_stat($link))); assert(mysqli_get_host_info($link) === $mysqli->host_info); printf("mysqli->host_info = '%s'/%s ('%s'/%s)\n", $mysqli->host_info, gettype($mysqli->host_info), mysqli_get_host_info($link), gettype(mysqli_get_host_info($link))); /* note that the data types are different */ assert(mysqli_info($link) == $mysqli->info); printf("mysqli->info = '%s'/%s ('%s'/%s)\n", $mysqli->info, gettype($mysqli->info), mysqli_info($link), gettype(mysqli_info($link))); assert(mysqli_thread_id($link) > $mysqli->thread_id); assert(gettype($mysqli->thread_id) == gettype(mysqli_thread_id($link))); printf("mysqli->thread_id = '%s'/%s ('%s'/%s)\n", $mysqli->thread_id, gettype($mysqli->thread_id), mysqli_thread_id($link), gettype(mysqli_thread_id($link))); assert(mysqli_get_proto_info($link) === $mysqli->protocol_version); printf("mysqli->protocol_version = '%s'/%s ('%s'/%s)\n", $mysqli->protocol_version, gettype($mysqli->protocol_version), mysqli_get_proto_info($link), gettype(mysqli_get_proto_info($link))); assert(mysqli_get_server_info($link) === $mysqli->server_info); printf("mysqli->server_info = '%s'/%s ('%s'/%s)\n", $mysqli->server_info, gettype($mysqli->server_info), mysqli_get_server_info($link), gettype(mysqli_get_server_info($link))); assert(mysqli_get_server_version($link) === $mysqli->server_version);
function _task_post_finish($link, $logData) { require 'config_files.php'; // TODO: Need to test the parameters in the request to make sure // they identify a valid task to finish // check the parameters $thisParam = 'sessionId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $sessionId = $logData[$thisParam]; } } else { $badParam[$thisParam] = "Missing"; } $thisParam = 'taskId'; if (array_key_exists($thisParam, $logData)) { if (!is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Not a number"; } else { $taskId = $logData[$thisParam]; } } else { $taskId = 0; } if (empty($badParam)) { // get the config record for this task & session if ($taskId == -1) { // -1 ==> get the latest task config for this session $query = 'SELECT * FROM ' . $DB_TABLE_SESSION_CONFIG . ' WHERE sessionId = ' . $sessionId . ' ORDER BY taskId DESC LIMIT 1'; } else { if ($taskId > 0) { // get the config record by session and task $query = 'SELECT * FROM ' . $DB_TABLE_SESSION_CONFIG . ' WHERE sessionId = ' . $sessionId . ' AND taskId = ' . $taskId; } else { // task == 0 is not supported yet // bad parameter $localErr = ''; $localErr['message'] = 'Bad parameter in finish request.'; $localErr['paramError']['taskId'] = "Cannot be 0"; $localErr['request'] = $logData; // $errData['globals'] = $GLOBALS; $errData['validation'] = $localErr; } } if (!empty($query)) { $result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 1) { if ($thisTaskRecord = mysqli_fetch_assoc($result)) { if ($taskId <= 0) { // set the current task $taskId = $thisTaskRecord['taskId']; // continue } } else { $thisTaskRecord = null; } } else { // no records $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading session configuration'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['queryConfig'] = $localErr; } // finish the session specified in the request $finishTime = time(); $finishTimeText = date('Y-m-d H:i:s', $finishTime); // TODO: Need to check to see if this has been closed, already. // if so, return an error, otherwise, update the record. // for now, we'll just only update unfinished records. $query = 'UPDATE ' . $DB_TABLE_SESSION_LOG . ' SET endTime = "' . $finishTimeText . '" WHERE sessionId = ' . $sessionId . ' AND endTime IS NULL ' . ' AND taskId = ' . $taskId; $result = mysqli_query($link, $query); // $response['debug']['query'] = $query; // $response['debug']['result'] = $result; if ($result) { $rData = array(); $rData['taskId'] = $taskId; $rData['finishTime'] = $finishTimeText; $rData['finishPageHtml'] = $thisTaskRecord['finishPageHtml']; $rData['finishPageNextUrl'] = $thisTaskRecord['finishPageNextUrl']; $response['data'] = $rData; } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error finishing session_log entry'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['updateTime'] = $localErr; } } } else { // bad parameter $localErr = ''; $localErr['message'] = 'Bad parameter in finish request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $logData; // $errData['globals'] = $GLOBALS; $errData['validation'] = $localErr; } if (!empty($errData)) { $response['error'] = $errData; } return $response; }
function _log_post($link, $postData) { require 'config_files.php'; // add record to the appropriate log table // determine log type from variable name $logType = 'open'; if (!empty($postData[$logType])) { $logData = $postData[$logType]; // $logData contains an open data block $logTable = $DB_TABLE_OPEN_LOG; // TODO: Validate fields } else { // $logData contains an transition data block $logType = 'transition'; if (!empty($postData[$logType])) { $logData = $postData[$logType]; // process transition log request $logTable = $DB_TABLE_TRANSITION_LOG; // TODO: Validate fields } else { // unrecognized command $errData['message'] = 'Log type not recognized. Log type must be \'open\' or \'transition\''; $errData['postData'] = $postData; $errData['getData'] = $_GET; //$errData['globals'] = $GLOBALS; $response['error'] = $errData; } } if (!empty($logTable)) { // process the log request // make query string from the data structure // add server-generated fields to insert query $dbColList = 'recordSeq, serverTimestamp, recordType'; $dbValList = 'NULL, CURRENT_TIMESTAMP, \'' . $logType . '\''; // add the client-provided fields foreach ($logData as $dbCol => $dbVal) { isset($dbColList) ? $dbColList .= ', ' : ($dbColList = ''); isset($dbValList) ? $dbValList .= ', ' : ($dbValList = ''); $dbColList .= $dbCol; if (empty($dbVal) && strlen($dbVal) == 0) { $dbValList .= 'NULL'; } else { $dbValList .= '\'' . $dbVal . '\''; } } // everything goes into the transition log $queryString = 'INSERT INTO ' . $DB_TABLE_TRANSITION_LOG . ' (' . $dbColList . ') VALUES (' . $dbValList . ')'; $qResult = mysqli_query($link, $queryString); // $respDbg['globals'] = $GLOBALS; $respDbg['table'] = $logTable; $respDbg['queryString'] = $queryString; $respDbg['argData'] = $logData; $respDbg['columns'] = $dbColList; $respDbg['values'] = $dbValList; $response['debug'] = $respDbg; if (!$qResult) { // SQL ERROR $respData['sqlQuery'] = $query_string; $respData['result'] = 'Error logging data to OPEN log'; $respData['sqlError'] = mysqli_sqlstate($link); $respData['message'] = mysqli_error($link); $response['error'] = $respData; } else { // success $respData['result'] = $qResult; $respData['message'] = 'Log record added to ' . $logType . ' log'; $response['data'] = $respData; } } else { // no data base table name. // ** This could probably be factored out. $errData['message'] = 'Log database table name is empty.'; $errData['postData'] = $postData; $errData['getData'] = $_GET; //$errData['globals'] = $GLOBALS; $response['error'] = $errData; } return $response; }
function _task_post_start($link, $logData) { require 'config_files.php'; // check the parameters $thisParam = 'sessionId'; if (empty($logData[$thisParam]) || !is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Missing or not a number"; } if (empty($badParam)) { // no parameter errors, so start the task // save the finish time in case we need it to close the task later $finishTime = time(); $studySessionRecord = null; // get the current or most recent task, which is the last task entry for this session // it could be finished (endTime != NULL) or not. $query = 'SELECT * FROM ' . $DB_TABLE_SESSION_LOG . ' WHERE sessionId = ' . $logData['sessionId'] . ' ORDER BY startTime DESC LIMIT 1'; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) > 0) { if ($thisRecord = mysqli_fetch_assoc($result)) { $studySessionRecord = $thisRecord; $currentTask = $thisRecord['taskId']; //$response['debug']['logData'] = $studySessionRecord; } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading condition count record'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query1data'] = $localErr; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading condition count'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query1'] = $localErr; } // +++++ // Get all the config records for studyId and conditionId ordered by taskId DESC // iterate through them and: // from the first record, get the max task info // save all the task config records to use later // ----- // get the number of tasks for this study if (!empty($studySessionRecord)) { $query = 'SELECT * FROM ' . $DB_TABLE_STUDY_CONFIG . ' WHERE studyId = ' . $studySessionRecord['studyId'] . ' AND conditionId = ' . $studySessionRecord['conditionId'] . ' ORDER BY taskId DESC'; $result = mysqli_query($link, $query); $studyConfigRecords = array(); if (mysqli_num_rows($result) > 0) { $maxTask = 0; while ($thisRecord = mysqli_fetch_assoc($result)) { // from the first record, get the max task info if (empty($maxTask)) { $maxTask = $thisRecord['taskId']; } // save all the config records $studyConfigRecords[$thisRecord['taskId']] = $thisRecord; } if (count($studyConfigRecords) != $maxTask) { $localErr = ''; $localErr['configRecordCount'] = count($studyConfigRecords); $localErr['maxTask'] = $maxTask; $localErr['message'] = 'Task config record count mismatch. The tasks must be numbered in sequence starting with 1.'; $errData['taskConfig'] = $localErr; } // $response['debug']['studyConfig']['count'] = mysqli_num_rows($result); } else { // no records $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading condition count record'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query2data'] = $localErr; } } else { // unable to find session } //$response['debug']['rawData']['numTasks'] = $; //$response['debug']['rawData']['currentTask'] = $currentTask; // here we have the current task and the current session info // if there is no task, start the first one // if there's an open task, finish it and start the next one $closeLast = false; if (!empty($studyConfigRecords) && !empty($studySessionRecord)) { // see if the current task is the last one for the study if ($currentTask >= $maxTask) { $closeLast = true; $closeTask = $currentTask; $errData['lastTask'] = 'Task ' . $currentTask . ' is the last task in this study.'; } else { // close the last task if it was open and this is not the first task of the study session if (is_null($studySessionRecord['endTime']) && $currentTask != 0) { $closeLast = true; $closeTask = $currentTask; } // advance to next task $currentTask = $currentTask + 1; // start a new task and return a start response $newTaskRecord = array(); $newTaskRecord['recordSeq'] = NULL; $newTaskRecord['studyId'] = $studySessionRecord['studyId']; $newTaskRecord['sessionId'] = $studySessionRecord['sessionId']; $newTaskRecord['taskId'] = $currentTask; $newTaskRecord['conditionId'] = $studySessionRecord['conditionId']; $taskStartTime = time(); $newTaskRecord['startTime'] = date('Y-m-d H:i:s', $taskStartTime); //$response['debug']['newTask'] = $newTaskRecord; foreach ($newTaskRecord as $dbCol => $dbVal) { isset($dbColList) ? $dbColList .= ', ' : ($dbColList = ''); isset($dbValList) ? $dbValList .= ', ' : ($dbValList = ''); $dbColList .= $dbCol; if (empty($dbVal) && strlen($dbVal) == 0) { $dbValList .= 'NULL'; } else { $escapedString = str_replace("'", "''", $dbVal); $dbValList .= '\'' . $escapedString . '\''; } } $queryString = 'INSERT INTO ' . $DB_TABLE_SESSION_LOG . ' (' . $dbColList . ') VALUES (' . $dbValList . ')'; $qResult = mysqli_query($link, $queryString); if (!$qResult) { // SQL ERROR $localErr['sqlQuery'] = $queryString; $localErr['result'] = 'Error creating new task record in session_log'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['writeError'] = $localErr; } else { // format start response buffer $sessionBuff['studyId'] = $newTaskRecord['studyId']; $sessionBuff['sessionId'] = $newTaskRecord['sessionId']; $sessionBuff['taskId'] = $newTaskRecord['taskId']; $sessionBuff['conditionId'] = $newTaskRecord['conditionId']; $sessionBuff['startTime'] = $newTaskRecord['startTime']; $sessionBuff['startPageHtml'] = ""; $sessionBuff['startPageNextUrl'] = ""; $response['data'] = $sessionBuff; } // if new task started, create corresponding session config record if ($qResult) { // create a new session_cofig record for this session if (!empty($studyConfigRecords)) { $studyTaskConfig = $studyConfigRecords[$currentTask]; $studyTaskConfig['recordSeq'] = NULL; $studyTaskConfig['sessionId'] = $newTaskRecord['sessionId']; // add server-generated fields to insert query $dbColList = 'autoConditionId'; $dbValList = '0'; // add the client-provided fields foreach ($studyTaskConfig as $dbCol => $dbVal) { isset($dbColList) ? $dbColList .= ', ' : ($dbColList = ''); isset($dbValList) ? $dbValList .= ', ' : ($dbValList = ''); $dbColList .= $dbCol; if (empty($dbVal) && strlen($dbVal) == 0) { $dbValList .= 'NULL'; } else { $escapedString = str_replace("'", "''", $dbVal); $dbValList .= '\'' . $escapedString . '\''; } } $queryString = 'INSERT INTO ' . $DB_TABLE_SESSION_CONFIG . ' (' . $dbColList . ') VALUES (' . $dbValList . ')'; $qResult = mysqli_query($link, $queryString); if (!$qResult) { // SQL ERROR $localErr = ''; $localErr['sqlQuery'] = $queryString; $localErr['result'] = 'Error creating session_config record'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['insert1'] = $localErr; } else { // finish start response buffer $response['data']['startPageHtml'] = $studyTaskConfig['startPageHtml']; $response['data']['startPageNextUrl'] = $studyTaskConfig['startPageNextUrl']; $response['data']['finishPageNextUrl'] = $studyTaskConfig['finishPageNextUrl']; $response['data']['taskType'] = $studyTaskConfig['taskType']; } } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error creating session_log entry'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['update1'] = $localErr; } } } else { // record not found $localErr = ''; $localErr['message'] = 'Config or session log record not found'; $localErr['configRecord'] = !empty($studyConfigRecords) ? $studyConfigRecords : null; $localErr['sessionRecord'] = !empty($studySessionRecord) ? $studySessionRecord : null; $errData['configOrLog'] = $localErr; } if ($closeLast) { // else if doing last task, // close last task, if open // finish the session specified in the request $finishTimeText = date('Y-m-d H:i:s', $finishTime); // TODO: Need to check to see if this has been closed, already. // if so, return an error, otherwise, update the record. // close the task record for this session $query = 'UPDATE ' . $DB_TABLE_SESSION_LOG . ' SET endTime = "' . $finishTimeText . '" WHERE sessionId = ' . $logData['sessionId'] . ' AND taskId = ' . $closeTask; $result = mysqli_query($link, $query); // $response['debug']['query'] = $query; // $response['debug']['result'] = $result; if ($result) { $localErr = ''; $localErr['finishTime'] = $finishTimeText; $localErr['taskId'] = $closeTask; $localErr['message'] = 'The previous task was not finished.'; $errData['openTask'] = $localErr; } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error finishing session_log entry'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['update1'] = $localErr; } } } else { //bad parameter $closeLast = false; $localErr = ''; $localErr['message'] = 'Bad parameter in start request.'; $localErr['paramError'] = $badParam; $localErr['request'] = $logData; // $errData['globals'] = $GLOBALS; $errData['validation'] = $localErr; } // something happened up there, but it was already reported if (!empty($errData)) { $response['error'] = $errData; } return $response; }
function _study_get_config($link, $logData) { require 'config_files.php'; // return the specified configuration // check the parameters $thisParam = 'studyId'; if (empty($logData[$thisParam]) || !is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Missing or not a number"; } $thisParam = 'conditionId'; if (empty($logData[$thisParam]) || !is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Missing or not a number"; } $thisParam = 'taskId'; if (empty($logData[$thisParam]) || !is_numeric($logData[$thisParam])) { $badParam[$thisParam] = "Missing or not a number"; } if (empty($badParam)) { // read conifguration for this study and condition $query = 'SELECT * FROM ' . $DB_TABLE_STUDY_CONFIG . ' WHERE studyId = ' . $logData['studyId'] . ' AND conditionId = ' . $logData['conditionId'] . ' AND taskId = ' . $logData['taskId']; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 1) { if ($thisRecord = mysqli_fetch_assoc($result)) { // remove the recordSeq field unset($thisRecord['recordSeq']); $response['data'] = array_merge($thisRecord); foreach ($response['data'] as $k => $v) { // set "null" strings to null values if ($v == 'NULL') { $response['data'][$k] = NULL; } } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Error reading config query'; $localErr['dataRecord'] = $thisRecord; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['queryData'] = $localErr; $response['error'] = $errData; } } else { $localErr = ''; $localErr['sqlQuery'] = $query; $localErr['result'] = 'Reading study config returned ' . mysqli_num_rows($result) . ' records'; $localErr['sqlError'] = mysqli_sqlstate($link); $localErr['message'] = mysqli_error($link); $errData['query'] = $localErr; $response['error'] = $errData; } } else { // bad parameter in request data $errData['message'] = 'Bad parameter in request.'; $errData['paramError'] = $badParam; $errData['request'] = $logData; // $errData['globals'] = $GLOBALS; $response['error'] = $errData; } return $response; }