duanlie2709 2014-04-29 22:45
浏览 88
已采纳

如何在PHP中显示MySQL查询的执行时间?

I am working on a PHP application that takes queries in a text box and returns paginated results. As part of the application I want to report the running time of the query.

Here is what I have done so far.

I started off by enabling profiling in by directly entering in the text box and running the script:

set global profiling = 1

Using the provided text box I enter the following query:

select @@profiling

And get:

1

Finally, I run the query as so:

select * from log

However, when I run the command for to profile the query:

show profiles

I receive no result and no content displayed on the page.

Since I see no table after the command "show profiles" does this mean that there are not sufficient privileges or am I missing another step?

I followed the procedure on:

Measuring actual MySQL query time

Please advise.

My PHP code is as follows:

<?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>

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongyongyin5339 2014-04-30 01:43
    关注

    This worked like a charm!

        $db->query('set profiling=1'); //optional if profiling is already enabled
        $db->query($_POST['query']);
        $stmt = $db->query('show profiles');
        $db->query('set profiling=0'); //optional as well
    
        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
        $errmsg = $stmt->errorInfo()[2]; //Output the error message 
    

    UPDATE (The following now works on innodb on my present setup)

    $db->query('set profiling=1'); //optional if profiling is already enabled
    $db->query($_POST['query']);
    $res = $db->query('show profiles');
    $records = $res->fetchAll(PDO::FETCH_ASSOC);
    $duration = $records[0]['Duration'];  // get the first record [0] and the Duration column ['Duration'] from the first record
    

    Result of (show profiles) from phpmyadmin.

    Query_ID    Duration    Query   
    1           0.00010575  SELECT DATABASE()
    

    Getting the actual (absolute) execution time of the last query in PHP (excluding network latency etc)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存