function do_sql($query) { global $SID; $dbh = $SID['dbh']; // do some cleanup and input checking $query = trim($query); // trim leading and trailing spaces $query_list = split_queries($query); // 2.1 - instead of explode $qcount = count($query_list); $SID['query_start_time'] = microtime(TRUE); $stmt_count = 0; $select_row_count = 0; $affected_row_count = 0; $select_qcount = 0; $non_select_qcount = 0; foreach ($query_list as $k => $query) { $query = strip_sql_comments($query); $qlen = strlen($query); if ($qlen < 1) { continue; } else { $stmt_count++; } // debug -- display the query // message('%d: [%s]', $stmt_count, $query); if (is_select($query)) { // select statement try { $sth = $dbh->prepare($query); if ($sth) { $sth->execute(); $rc = select_results($sth, $qcount, $k + 1); if ($rc) { $select_row_count += $rc; } if (DBENGINE == 'pgsql') { // count affected rows for PostgreSQL // note: must subtract $rc (returned rows) because // PDO/pgsql incorrectly counts rows returned by SELECT statements as affected rows $affected_row_count += $sth->rowCount() - $rc; } } } catch (PDOException $e) { // 2014-03-27 bw -- MySQL is putting out some bogus "general error" errors if ($e->getCode() != 'HY000') { error_message('query #%d: %s', $k + 1, $e->getMessage()); } } $select_qcount++; } else { // non-select statement try { $sth = $dbh->prepare($query); if ($sth) { $sth->execute(); $affected_row_count += $sth->rowCount(); } } catch (PDOException $e) { error_message('query #%d: %s', $k + 1, $e->getMessage()); } $non_select_qcount++; } } // report statistics on results $elapsed_time = microtime(TRUE) - $SID['query_start_time']; $messages = array(); if ($stmt_count > 1) { array_push($messages, sprintf('%s queries performed', number_format($stmt_count))); } if ($affected_row_count) { array_push($messages, sprintf('%s rows affected', number_format($affected_row_count))); } if ($select_row_count) { array_push($messages, sprintf('%s rows returned', number_format($select_row_count))); } array_push($messages, sprintf('elapsed time: %s milliseconds', number_format($elapsed_time * 1000, 2))); message(join('; ', $messages) . '.'); }
* THE SOFTWARE. */ /** * @global integer|array $seg_id * @uses main.js * The handlerData property of the GOOGLEMAP object calls a $.getJSON() function that loops through * an array of Strava segment IDs. Those IDs are POST(ed) here. */ $seg_id = $_POST['key']; $db = connect(); /** * @var object $db Variable to store the database connection object. */ insert_segment($db, $seg_id); insert_leaderboard($db, $seg_id); select_results($db, $seg_id); /** * @method object connect() Creates our database connection object. * @param string $host Reference to the HOST constant that contains server host name. * @param string $dbname Reference to the STRAVA_DB constant that contains database name. * @param string $user Reference to the USER constant that contains user name. * @param string $pass Reference to the PASSWORD constant that contains database password. * @var object $db Stores the new mysqli database object created from above parameters. * @return object Our Database connection object. */ function connect() { require '//home/heatery/credentials/connect.php'; $host = DB_HOST_CSQ2; $dbname = STRAVA_DB; $user = DB_USER;
function do_sql($query) { global $SID; $dbh = $SID['dbh']; // do some cleanup and input checking $query = trim($query); // trim leading and trailing spaces $query_list = split_queries($query); // 2.1 - instead of explode $qcount = count($query_list); $SID['query_start_time'] = microtime(TRUE); $stmt_count = 0; $select_row_count = 0; $non_select_row_count = 0; $select_qcount = 0; $non_select_qcount = 0; foreach ($query_list as $k => $query) { $query = strip_sql_comments($query); $qlen = strlen($query); if ($qlen < 1) { continue; } else { $stmt_count++; } // debug -- display the query // message('%d: [%s]', $stmt_count, $query); if (is_select($query)) { // select statement try { $sth = $dbh->prepare($query); if ($sth) { $sth->execute(); $rc = select_results($sth, $qcount, $k + 1); if ($rc) { $select_row_count += $rc; } } } catch (PDOException $e) { error_message('query #%d: %s', $k + 1, $e->getMessage()); } $select_qcount++; } else { // non-select statement try { $sth = $dbh->prepare($query); if ($sth) { $sth->execute(); $non_select_row_count += $sth->rowCount(); } } catch (PDOException $e) { error_message('query #%d: %s', $k + 1, $e->getMessage()); } $non_select_qcount++; } } // report statistics on results $elapsed_time = microtime(TRUE) - $SID['query_start_time']; $messages = array(); if ($stmt_count > 1) { $qstr = 'queries'; array_push($messages, sprintf('%s %s performed', number_format($stmt_count), $qstr)); } else { $qstr = 'query'; } if ($non_select_row_count) { array_push($messages, sprintf('%d non-select %s affected %s rows', $non_select_qcount, $qstr, number_format($non_select_row_count))); } if ($select_row_count) { array_push($messages, sprintf('%d select %s returned %s rows', $select_qcount, $qstr, number_format($select_row_count))); } array_push($messages, sprintf('elapsed time: %s milliseconds', number_format($elapsed_time * 1000, 2))); message(join('; ', $messages) . '.'); }