【问题标题】:multi query + single query多查询+单查询
【发布时间】:2012-08-01 19:46:43
【问题描述】:

如何设置这个功能?最后一个查询没有被执行..我在一个循环中搜索了很多查询(foreach,for,while)但什么都没有..我正在尝试存储会话。

private function gc($expire)
{
  $gcq = "SELECT `path`, `last`, LENGTH(`path`) FROM `sessions` WHERE LENGTH(`path`) > 0  AND DATE_ADD(`last`, INTERVAL ".(int) $expire." SECOND) < NOW();";
  $gcq .= "DELETE FROM `sessions` WHERE DATE_ADD(`last`, INTERVAL ".(int) $expire." SECOND) < NOW()";
            if($this->dbh->multi_query($gcq)) 
              {
                $arr_gc = null;
                $count = 0;
                do {
                      if($result = $this->dbh->store_result()) 
                      {
                        while($row = $result->fetch_assoc()) 
                        {
                         $arr_gc[$count] = array($row['path'], $row['last']);
                         $count++;
                        }
                        $result->free();
                      }
                      if($this->dbh->more_results()) 
                      {
                        $garbage = null;
                      }
                    } 
                     while($this->dbh->next_result());
                   }
                    // no problems up here..
                    // problems from here to end....         

                    $alfa = count($arr_gc);

                    if($alfa > 0)
                    {
                      $count = 0;

                        while($count < $alfa)
                        {
                         $this->dbh->query("INSERT INTO `store_sess` SET `xpath` = '".$this->dbh->real_escape_string($arr_gc[$count][0])."', in = '".$this->dbh->real_escape_string($arr_gc[$count][1])."', out = '0000-00-00 00:00:00'");
                         $count++;
                        }                        
                    }
  return $this->dbh->affected_rows;
}

编辑: store_sess 表结构:

id int (autoincrement)
xpath longtext
in datetime (tried also with varchar)
out varchar 

【问题讨论】:

  • 你确定$alfa > 0,对吧?
  • 是的,现在我正在使用 alfa = 2 进行调试

标签: php mysqli


【解决方案1】:
while($count < $alfa) {
    $this->dbh->query("INSERT INTO `store_sess` SET `xpath` = '".$this->dbh->real_escape_string($arr_gc[$count][0])."', in = '".$this->dbh->real_escape_string($arr_gc[$count][1])."', out = '0000-00-00 00:00:00'");
    $count++;
} 

上面的代码不是很可扩展。相反,您应该完整地设置查询(您可以使用一个查询进行多次插入)并在设置查询字符串后执行它。

更新

您的while 循环也是不必要的。

结束更新

$insertVals = "";
for($count = 0; $count < $alfa, $count++) {
    $insertVals = ($insertVals == "" ? "" : ", ") . 
        "('" . $this->dbh->real_escape_string($arr_gc[$count][0]) . "', '" . 
        $this->dbh->real_escape_string($arr_gc[$count][1]) . 
        "', '0000-00-00 00:00:00')";
}

$query = "INSERT INTO `store_sess` (`xpath`, `in`, `out`) VALUES " . $insertVals;

【讨论】:

  • .. 非常好的主意,谢谢.. 但是我用了一点点改变$insertVals .= ($insertVals == "" ? "" : ", ")..... ecc....(注意前面的点=)再见
猜你喜欢
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 2010-11-07
相关资源
最近更新 更多