我正在研究一个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