【问题标题】:class definative commands out of sync solution类定义命令不同步解决方案
【发布时间】:2016-06-29 19:52:53
【问题描述】:

经过大约一周的搜索,我似乎没有找到任何确定的/有效的/最新的/标准的方法。

显而易见的解决方案是运行查询,将结果放入变量中并为下一次查询做好准备——为了提高效率,不要关闭连接。

这是在具有 select / update / insert 的包装类中完成的,该类依次调用一个或多个查询(对于 mysqli multi_query() ),因此理想情况下,最好在查询 / 查询中的 next 之前清除任何结果以确保它运行时无需担心之前发生的事情。 这似乎不可能,因为某些解决方案需要结果集。

当前选择/更新/插入调用:

 private function queryclearup($dataresource) {
        //clear up if required
        switch ($this->dbtype) {
             case "mysqli":
                    @mysqli_next_result($this->connection);
                    @mysqli_free_result($dataresource); //clear result to avoid out of sync issues
                    break;
        }
 }

在处理完结果集之后——例如在选择的情况下转换为数据数组,或者在更新/插入的情况下只记录一个错误。

这个问题仍然存在于某些(更新),但不是其他(选择/插入)。

 public function select($query, $args = array()) {
        $this->loghelper->write("SQL Select\n" . $query);
        $args = _PHP::array_defaults($args, array(
                                "pk" => null
        ));
        $data_table = array();
        $errors = null;
        $query = utf8_decode($query);
        switch ($this->dbtype) {
             case "mysqli":
                    $dbdata = $this->query($query);
                    $data_table = $this->resourceToDataTable($dbdata, $args);
                    break;
        }
        return $data_table;
 }

然后

private function resourceToDataTable($dataresource, $args = array()) {
            $data_table = array();
            if (gettype($dataresource) == "resource" || gettype($dataresource) == "object") {
                 do {
                        switch ($this->dbtype) {
                             case "mysqli":
                                    $row = $dataresource->fetch_assoc();
                                    break;
                        }
                        if ($row) {
                             array_push($data_table, $row);
                        }
                 } while ($row);
                 if (!empty($args["pk"])) {
                        $pktable = array();
                        foreach ($data_table as $d) {
                             if (!array_key_exists($args["pk"], $pktable)) {
                                    $pktable[$d[$args["pk"]]] = $d;
                             }
                        }
                        $data_table = $pktable;
                 }
            }
            $this->queryclearup($dataresource);
            return $data_table;
     }

【问题讨论】:

  • 您只移动到下一个结果。如果还有其他人呢?
  • 如果它是一个选择,它们已经被循环通过,其他的应该只是布尔值?

标签: php nginx mysqli


【解决方案1】:

这似乎是必需的

  private function queryclearup($dataresource) {
    //clear up if required
    switch ($this->db["contype"]) {
      case "mysqli":
        while ($dump = @mysqli_next_result($this->connection)) {
          $donothing = 1;
        }
        @mysqli_free_result($dataresource); //clear result to avoid out of sync issues
        break;
    }
  }

【讨论】:

    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 2011-02-05
    • 2011-03-24
    • 2020-11-17
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多