| 5 database.pgsql.inc | _db_query($query, $debug = 0) |
| 5 database.mysql.inc | _db_query($query, $debug = 0) |
| 5 database.mysqli.inc | _db_query($query, $debug = 0) |
| 6 database.mysql.inc | _db_query($query, $debug = 0) |
| 6 database.pgsql.inc | _db_query($query, $debug = 0) |
| 6 database.mysqli.inc | _db_query($query, $debug = 0) |
Helper function for db_query().
Related topics
8 calls to _db_query()
File
- includes/
database.mysql.inc, line 98 - Database interface code for MySQL database servers.
Code
function _db_query($query, $debug = 0) {
global $active_db, $queries, $user;
if (variable_get('dev_query', 0)) {
list($usec, $sec) = explode(' ', microtime());
$timer = (float) $usec + (float) $sec;
// If devel.module query logging is enabled, prepend a comment with the username and calling function
// to the SQL string. This is useful when running mysql's SHOW PROCESSLIST to learn what exact
// code is issueing the slow query.
$bt = debug_backtrace();
// t() may not be available yet so we don't wrap 'Anonymous'.
$name = $user->uid ? $user->name : variable_get('anonymous', 'Anonymous');
// str_replace() to prevent SQL injection via username or anonymous name.
$name = str_replace(array('*', '/'), '', $name);
$query = '/* ' . $name . ' : ' . $bt[2]['function'] . ' */ ' . $query;
}
$result = mysql_query($query, $active_db);
if (variable_get('dev_query', 0)) {
$query = $bt[2]['function'] . "\n" . $query;
list($usec, $sec) = explode(' ', microtime());
$stop = (float) $usec + (float) $sec;
$diff = $stop - $timer;
$queries[] = array($query, $diff);
}
if ($debug) {
print '<p>query: ' . $query . '<br />error:' . mysql_error($active_db) . '</p>';
}
if (!mysql_errno($active_db)) {
return $result;
}
else {
// Indicate to drupal_error_handler that this is a database error.
${DB_ERROR} = TRUE;
trigger_error(check_plain(mysql_error($active_db) . "\nquery: " . $query), E_USER_WARNING);
return FALSE;
}
}
Login or register to post comments
Comments
micro optimization
Lines 6-7 may use a bit of improvement.
<?phplist($usec, $sec) = explode(' ', microtime());
$timer = (float) $usec + (float) $sec;
?>
is the same as
<?php$timer = microtime(true);
?>
Since PHP5 the get_as_float parameter was added.
http://www.php.net/manual/en/function.microtime.php
micro optimization 2
Similarly, the following part
<?phplist($usec, $sec) = explode(' ', microtime());
$stop = (float) $usec + (float) $sec;
$diff = $stop - $timer;
$queries[] = array($query, $diff);
?>
can be replaced with
<?php$queries[] = array($query, microtime(TRUE) - $timer);
?>
p.s. I just noticed the Naming Conventions -> Constants section saying that "pre-defined PHP constants like TRUE.. should always be all-uppercase".