我正在尝试将一些执行时间报告添加到我附加到页面末尾的诊断中。我有一个处理数据库连接和查询执行的函数:
<?php
// get_data.php
function get_data($sql_statement, $sql_bind_variables=array(), $sql_result_type="cursor", $sql_result_cursor=":results_cursor") {
$time_start = microtime(true);
//tie-in to the existing $connection
global $connection;
global $debugging;
if($debugging['show_debugging_ind']){
global $variables;
$variables['debugging_sql']=$sql_statement;
};
//since we have to determine if the sql result is packaged within a cursor multiple times, let's perform the slow text comparison once, and reference the result in multiple places.
$cursor_ind=0;
if ($sql_result_type=="cursor") {
$cursor_ind=1;
}
if ($cursor_ind) {
$returned_cursor = oci_new_cursor($connection);
if (!$returned_cursor) {
$e = oci_error($connection);
trigger_error('
<h2>Could not create new cursor:</h2>
<div class="row-fluid">
<div class="span1">Connection</div>
<div class="span11"><pre>'.$connection.'</pre></div>
</div>
<div class="row-fluid">
<div class="span1">Message</div>
<div class="span11"><pre class="text-error">'.$e['message'],E_USER_ERROR.'</pre></div>
</div>');
}
}
$sql_parsed=oci_parse($connection, $sql_statement);
if (!$sql_parsed) {
$e = oci_error($connection);
trigger_error('
<div id="parse_error" class="clearfix">
<h2>Could not parse statement:</h2>
<div class="row-fluid">
<div class="span1">SQL</div>
<div class="span11"><pre>'.$sql_parsed.'</pre></div>
</div>
<div class="row-fluid">
<div class="span1">Message</div>
<div class="span11"><pre class="text-error">'.$e['message'],E_USER_ERROR.'</pre></div>
</div>
</div>');
}
if ($cursor_ind) {
oci_bind_by_name($sql_parsed, $sql_result_cursor, $returned_cursor, -1, OCI_B_CURSOR);
}
//loop over the array of bind variables
foreach($sql_bind_variables AS $bind_variable_name => $bind_variable_value) {
// echo "<p>processing ".$bind_variable_name.", value=".$sql_bind_variables[$bind_variable_name]."<br />";
// echo "oci_bind_by_name(sql_parsed, ".$bind_variable_name.", ".$sql_bind_variables[$bind_variable_name].");</p>";
oci_bind_by_name($sql_parsed, $bind_variable_name, $sql_bind_variables[$bind_variable_name]);
}
attempt_execute($sql_parsed);
if ($cursor_ind) {
oci_execute($returned_cursor);
oci_fetch_all($returned_cursor, $sql_results, null, null, OCI_FETCHSTATEMENT_BY_ROW);
}
else {
oci_fetch_all($sql_parsed, $sql_results, null, null, OCI_FETCHSTATEMENT_BY_ROW);
}
oci_free_statement($sql_parsed);
if ($cursor_ind) {
oci_free_statement($returned_cursor);
}
return $sql_results;
if($debugging['show_debugging_ind']){
$time_end = microtime(true);
$variables['execution_time']=$time_end-$time_start;
};
}
?>
因此,每次查询都会调用此get_data()
函数,如果$debugging['show_debugging_ind']
位被翻转,我会存储其他一些信息:
$get_my_cases=get_data('BEGIN pkg_common.get_cases(:results_cursor,p_type => :p_type,P_USER_ID => :P_USER_ID); END;', array(':p_type'=>'INBOX',':P_USER_ID'=>$variables['username']));
if($debugging['show_debugging_ind']){
$debugging['queries']['get_my_cases']['sql']=$variables['debugging_sql'];
$debugging['queries']['get_my_cases']['results']=$get_my_cases;
$debugging['queries']['get_my_cases']['execution_time']=$variables['execution_time'];
};
$get_team_cases=get_data('BEGIN pkg_common.get_cases(:results_cursor, p_type => :p_type, P_USER_ID => :P_USER_ID, P_ASSIGNED_TO_LOC_CD => :P_ASSIGNED_TO_LOC_CD); END;',array(':p_type'=>'INBOX', ':P_USER_ID'=>$variables['username'], ':P_ASSIGNED_TO_LOC_CD'=>$variables['post']));
if($debugging['show_debugging_ind']){
$debugging['queries']['get_team_cases']['sql']=$variables['debugging_sql'];
$debugging['queries']['get_team_cases']['results']=$get_team_cases;
$debugging['queries']['get_team_cases']['execution_time']=$variables['execution_time'];
};
在我的诊断中,sql文本($variables['debugging_sql']
)正在更新并显示效果很好,但$variables['execution_time']
时间变量要么没有设置,要么为所有查询获得相同的值。我做错了什么?
答案 0 :(得分:2)
将$variables['execution_time']
的设置移至return
语句之前:
<?php
// get_data.php
function get_data($sql_statement, $sql_bind_variables=array(), $sql_result_type="cursor", $sql_result_cursor=":results_cursor") {
$time_start = microtime(true);
//tie-in to the existing $connection
global $connection;
//since we have to determine if the sql result is packaged within a cursor multiple times, let's perform the slow text comparison once, and reference the result in multiple places.
$cursor_ind=0;
if ($sql_result_type=="cursor") {
$cursor_ind=1;
}
if ($cursor_ind) {
$returned_cursor = oci_new_cursor($connection);
if (!$returned_cursor) {
$e = oci_error($connection);
trigger_error('
<h2>Could not create new cursor:</h2>
<div class="row-fluid">
<div class="span1">Connection</div>
<div class="span11"><pre>'.$connection.'</pre></div>
</div>
<div class="row-fluid">
<div class="span1">Message</div>
<div class="span11"><pre class="text-error">'.$e['message'],E_USER_ERROR.'</pre></div>
</div>');
}
}
$sql_parsed=oci_parse($connection, $sql_statement);
if (!$sql_parsed) {
$e = oci_error($connection);
trigger_error('
<div id="parse_error" class="clearfix">
<h2>Could not parse statement:</h2>
<div class="row-fluid">
<div class="span1">SQL</div>
<div class="span11"><pre>'.$sql_parsed.'</pre></div>
</div>
<div class="row-fluid">
<div class="span1">Message</div>
<div class="span11"><pre class="text-error">'.$e['message'],E_USER_ERROR.'</pre></div>
</div>
</div>');
}
if ($cursor_ind) {
oci_bind_by_name($sql_parsed, $sql_result_cursor, $returned_cursor, -1, OCI_B_CURSOR);
}
//loop over the array of bind variables
foreach($sql_bind_variables AS $bind_variable_name => $bind_variable_value) {
// echo "<p>processing ".$bind_variable_name.", value=".$sql_bind_variables[$bind_variable_name]."<br />";
// echo "oci_bind_by_name(sql_parsed, ".$bind_variable_name.", ".$sql_bind_variables[$bind_variable_name].");</p>";
oci_bind_by_name($sql_parsed, $bind_variable_name, $sql_bind_variables[$bind_variable_name]);
}
attempt_execute($sql_parsed);
if ($cursor_ind) {
oci_execute($returned_cursor);
oci_fetch_all($returned_cursor, $sql_results, null, null, OCI_FETCHSTATEMENT_BY_ROW);
}
else {
oci_fetch_all($sql_parsed, $sql_results, null, null, OCI_FETCHSTATEMENT_BY_ROW);
}
oci_free_statement($sql_parsed);
if ($cursor_ind) {
oci_free_statement($returned_cursor);
};
global $debugging;
if($debugging['show_debugging_ind']){
global $variables;
$time_end = microtime(true);
$variables['execution_time']=$time_end-$time_start;
$variables['debugging_sql']=$sql_statement;
};
return $sql_results;
}
?>