我正在研究一個PHP應用程序,它在文本框中接受查詢並返回分頁結果。作爲應用程序的一部分,我想報告查詢的運行時間。如何在PHP中顯示MySQL查詢的執行時間?
這是我迄今爲止所做的。
我開始通過啓用在文本框中直接輸入並運行該腳本貌相:
set global profiling = 1
使用所提供的文本框中我輸入以下查詢:
select @@profiling
並獲得:
1
最後,我運行查詢如下:
select * from log
然而,當我運行來分析查詢的命令:
show profiles
我沒有收到結果,並沒有在網頁上顯示的內容。
因爲我看到命令「show profiles」之後沒有表格,這是否意味着沒有足夠的特權或者我錯過了另一個步驟?
我也跟着上的程序:
Measuring actual MySQL query time
請指教。
我的PHP代碼如下:
<?php
if($_POST)
{
$db = new PDO('mysql:host=localhost;dbname=<dbname>;charset=utf8', 'user', 'pass');
$stmt = $db->prepare($_POST['query']);
$stmt->execute();
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
$errmsg = $stmt->errorInfo()[2]; //Output the error message - Index 2 of the array
echo $errmsg;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Log</title>
</head>
<body>
<form method="post" id="queryform">
<div class="label">
<span class="label">Enter SQL Query</span>
</div>
<div class="input">
<input type="text" name="query" size="150" value="<?=$_POST['query']?>" />
</div>
</form>
<? if (isset($records)): ?>
<table border="1">
<? foreach($records as $record): ?>
<tr>
<? foreach($record as $colname => $value): ?>
<td>
<?=$value;?>
</td>
<? endforeach; ?>
</tr>
<? endforeach; ?>
</table>
<? endif; ?>
</body>
</html>
任何幫助,將不勝感激。
謝謝你的答覆。這是唯一可行的方法嗎?我這樣做並沒有想到它可能是唯一的工作解決方案。我感謝您的幫助。 – Vahe
正確的信息應該是「」這個查詢花了「。 「($ end - $ start)。」秒。「# –
@MarcioMazzucato你說得對,'microtime(true)'在幾秒鐘內返回一個浮點數。 – marcvangend