/** * 执行SQL语句 * * @param string $sql * @return resource */ public static function query($sql) { $timestamp = microtime(true); $r = mysqli_query(SQL::$connection, $sql); $spent = round((microtime(true) - $timestamp) * 1000, 3); if ($r) { DEBUG::put('Query: ' . $sql . ' spent: ' . $spent . 'ms', 'MySQL'); } else { DEBUG::put('Query: ' . $sql . ' fail: #' . SQL::errno() . ' ' . SQL::errmsg() . ' spent: ' . $spent, 'MySQL'); } return $r; }
$count = 0; while ($tokens = fgetcsv($handle, 2048, $delimiter, $enclosure)) { // insert one record at a time $query = "INSERT INTO " . SQL::escape($_REQUEST['table_name']) . " (" . $headers . ") VALUES ("; // use all provided tokens $index = 0; foreach ($tokens as $token) { if ($index++) { $query .= ', '; } $query .= "'" . SQL::escape($token) . "'"; } // finalize the statement $query .= ')'; // execute the statement if (!SQL::query($query, TRUE) && SQL::errno()) { $context['text'] .= '<p>' . $here . ': ' . $query . BR . SQL::error() . "</p>\n"; } $queries++; // ensure we have enough time if (!($queries % 50)) { Safe::set_time_limit(30); } } // clear the cache Cache::clear(); // report of script data $time = round(get_micro_time() - $context['start_time'], 2); $context['text'] .= '<p>' . sprintf(i18n::s('%d SQL statements have been processed in %.2f seconds.'), $queries, $time) . '</p>'; // remember this in log as well Logger::remember('tables/import.php: Data has been imported into ' . $_REQUEST['table_name'], $queries . ' SQL statements have been processed in ' . $time . ' seconds.');
/** * query the database * * This function populates the error context, where applicable. * * @param string the SQL query * @param boolean optional TRUE to not report on any error * @param resource connection to be considered, if any * @return the resource returned by the database server, or the number of affected rows, or FALSE on error */ public static function query(&$query, $silent = FALSE, $connection = NULL) { global $context; // allow for reference $output = FALSE; // use the default connection if (!$connection) { // we do need a connection to the database if (!isset($context['connection']) || !$context['connection']) { return $output; } $connection = $context['connection']; } // reopen a connection if database is not reachable anymore if (get_micro_time() - $context['start_time'] > 1.0 && !SQL::ping($connection)) { // remember the error, if any -- we may not have a skin yet if (!$silent) { if (is_callable(array('Skin', 'error'))) { Logger::error(i18n::s('Connection to the database has been lost')); } else { die(i18n::s('Connection to the database has been lost')); } } // query cannot be processed return $output; } // ensure enough execution time Safe::set_time_limit(30); // profile database requests $query_stamp = get_micro_time(); // do the job if (is_callable('mysqli_query')) { $result = mysqli_query($connection, $query); } else { $result = mysql_query($query, $connection); } // finalize result if ($result) { // provide more than a boolean result if ($result === TRUE) { if (is_callable('mysqli_affected_rows')) { $result = mysqli_affected_rows($connection); } else { $result = mysql_affected_rows($connection); } } // flag slow requests $duration = get_micro_time() - $query_stamp; if ($duration >= 0.5 && $context['with_debug'] == 'Y') { Logger::remember('shared/sql.php: SQL::query() slow request', $duration . "\n\n" . $query, 'debug'); } // return the set of selected rows return $result; } // remember the error, if any if (SQL::errno($connection)) { // display some error message if (!$silent) { if (is_callable(array('Skin', 'error'))) { Logger::error($query . '<br />' . SQL::error($connection)); } else { die($query . '<br />' . SQL::error($connection)); } } // log the error at development host if ($context['with_debug'] == 'Y') { Logger::remember('shared/sql.php: SQL::query()', SQL::error($connection) . "\n\n" . $query, 'debug'); } } // no valid result return $output; }
/** * delete all documentation pages */ public static function purge() { global $context; // purge the old documentation $query = "DELETE FROM " . SQL::table_name('phpdoc'); if (SQL::query($query, TRUE) === FALSE && SQL::errno()) { echo $query . BR . SQL::error() . BR . "\n"; } }