如何获取查询执行的次数

时间:2015-05-14 05:58:50

标签: php mysql

在PHP中,有没有办法获得24小时内执行查询的总次数?

3 个答案:

答案 0 :(得分:1)

尝试增加一张桌子。让我们用列命名executetb

  • exidint主要自动增加
  • executionnumberint
  • datetimeexec列(应具有datetime数据类型)

如果您的网页是可访问的,那么您的系统应运行一个查询,以检查今天executetb中是否存在现有数据。在运行主查询之前备用当前的执行次数:

$today = date("Y-m-d");

if($stmt = $connection->prepare("SELECT exid, executionnumber FROM executetb WHERE DATE(datetimeexec) = ? ")){
  $stmt->bind_param("s",$today);
  $stmt->execute();
  $stmt->store_result();
  $check = $stmt->num_rows;

    if($check == 0){ /* IF NO EXISTING DATA FOR TODAY */
      $exact = date("Y-m-d H:i:s"); /* GET CURRENT DATE AND TIME */
      $executionnumber = 0; /* START WITH 0 */
      if($stmt2 = $connection->prepare("INSERT INTO executetb (executionnumber, datetimeexec) VALUES (?)")){
        $stmt2->bind_param("is",$executionnumber,$exact);
        $stmt2->execute();
        $exid = $stmt2->insert_id; /* GET THE INSERTED ID */
        $stmt2->close();
      } /* END OF CREATING A DATA FOR TODAY */
    } /* END OF IF NO EXISTING DATE FOR TODAY */

    else { /* ELSE, IF THERE IS ALREADY A DATA FOR TODAY */
      $stmt->bind_result($exid,$executionnumber); /* GET THE CURRENT EXECUTION NUMBER FOR TODAY */
      $stmt->fetch();
    }

  $stmt->close();
} /* END OF PREPARED STATEMENT */

执行查询时使用if()语句:

if($stmt = $connection->prepare("SELECT column FROM table")){ /* WHEN THE STATEMENT IS TRUE */
  $stmt->execute();
  $executionnumber = $executionnumber + 1; /* INCREMENT THE CURRENT NUMBER OF EXECUTION */
  mysqli_query($connection,"UPDATE executetb SET executionnumber = $executionnumber WHERE exid = '$exid'"); /* THEN UPDATE THE EXECUTION TABLE */
  $stmt->close();
}

功能:

要将其放入函数中,只需创建一个函数:

function execute($today){
  /* PUT HERE THE FIRST CODE I'VE PROVIDED EXCEPT FOR THE DATE TODAY */

  return $executionnumber;
}

并致电你的职能:

if(/* YOUR MAIN STATEMENT QUERY HERE */){

  $today = date("Y-m-d");
  $numberofexec = execute($today);
  $numberofexec = $numberofexec + 1;

  mysqli_query($con,"UPDATE executetb SET executionnumber = '$numberofexec' WHERE DATE(datetimeexec) = '$today'");

} /* END OF YOUR MAIN STATEMENT */

答案 1 :(得分:0)

在数据库中创建另一条记录并从零开始其值并编写另一个简单函数,以便在执行查询时调用updateCount(),updateCount()。并且updateCount()将有一个简单的SQL查询,每次执行查询主查询时,它将使记录增加1。

我希望这会有所帮助。

答案 2 :(得分:0)

你可以通过数量来获得它。查询在过去1天内在general_log中显示。例如- 首先启用通用日志&输出到表格。

SET global general_log = 1;
SET global log_output = 'table';

然后运行查询

select count(*) from mysql.general_log where timestampdiff(second,event_time,now()) <= 86400 and argument ='select * from test where id=4';
    +----------+
    | count(*) |
    +----------+
    |        3 |
    +----------+
    1 row in set (0.00 sec)