dronthpi05943 2017-09-22 03:16
浏览 23
已采纳

PHP运行一次查询

This may sound silly but i am trying to figure out the best approach to run a query only once in a stats table that have all values set to 0 as a starting point and i would like your opinion, my table is by example:

Col1 - Col2 - Col3 - Col4 - Created - Modified
0      0      0      0      Datetime- Datetime

My main script have several foreach loops where i evaluate some conditions, if there is a match, i build the query and run a mysqli multiquery at the end, by example (pseudo code):

$query = '';
foreach ($array as $val) {
  if ($val == $mycondition) {
    $query .= "UPDATE mytable SET Col1 = Col1 + 1;";   
  }
}

$db = mysqliConnection();
$num = $db->query("SELECT * FROM my table WHERE id = '".$ID."' AND Col1 = 0 AND Col2 = 0 AND Col3 = 0 AND Col4 = 0");

$result = mysqli_num_rows($num);
// if 0 it means that nothing has been modified yet so i can run the query
if ($result > 0) {
  $db->multi_query($query)
}
$db->close();

This is the best approach i can think of right now, i created the columns created and modified but i think they will not work for this, as the creation time will always be before the modified time, probably in milisecs (but i dont want to risk taking the path to allow the mutiquery run after max 3 sec or so, cause if the user run the page several time by mistake between 3 secs it will store wrong values) and the modified column will change as soon as the first value is updated making the next queries fail if i rely on that column as condition in the WHERE clause (even if are just milisec).

What do you think guys is the best approach or i am in the right path?

Best Regards

  • 写回答

1条回答 默认 最新

  • drq231358 2017-09-22 06:57
    关注

    I would suggest to use a flag. Something like:

    $queryFlag = 0;
    
    if($queryFlag == 0) {
         //Do you things here
         $queryFlag = 1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端