代码之家  ›  专栏  ›  技术社区  ›  Eli

mysqli->error:是只针对最后一个查询,还是针对查询组中的最后一个错误?

  •  1
  • Eli  · 技术社区  · 17 年前

    我是mysqli的新手,我试图确认,如果我这样做了,那么errno将被设置为最后一个错误(如果有),而不是最后一个查询的错误。

    谢谢

    $mysqli->autocommit(FALSE);
    
    $mysqli->query("INSERT INTO .....");
    $mysqli->query("INSERT INTO .....");
    $mysqli->query("INSERT INTO .....");
    $mysqli->query("INSERT INTO .....");
    $mysqli->query("INSERT INTO .....");
    
    if ( 0==$mysqli->errno ) {
        $mysqli->commit();
    } else {
        $mysqli->rollback();
        // Handle error
    }
    
    4 回复  |  直到 17 年前
        1
  •  4
  •   Bill Karwin    17 年前

    $mysqli->query() 也将示例从 mysqli_errno 文件:

    if (!$mysqli->query("INSERT ...")) {
        printf("Errorcode: %d\n", $mysqli->errno);
    }
    
        2
  •  3
  •   Ray    17 年前

    最近的函数调用 .

        3
  •  1
  •   Dev    15 年前

    不,您必须在每个查询之间签入,因为它只会为最后一个查询提供错误。。。所以,如果您的第一次查询失败,最后一次执行正确,那么您将不会得到错误。。。所以在所有查询之后逐个检查,而不是最后。。。

        4
  •  1
  •   user2176127 user2176127    13 年前

    class DBException extends Exception {
    }
    class DBConnectException extends DBException {
    }
    class DBQueryException extends DBException {
    }
    
    class DB extends MySQLi {
        private static $instance = null;
    
        private function __construct() {
            parent::__construct('host',
                                'username',
                                'passwd',
                                'dbname');
    
            if ($this->connect_errno) {
                throw new DBConnectException($this->connect_error, $this->connect_errno);
            }
        }
    
        private function __destructor() {
            parent::close();
        }
    
        private function __clone() {
        }
    
        public static function getInstance() {
            if (self::$instance == null) {
                self::$instance = new self();
            }
            return self::$instance;
        }
    
        public function query($query, $resultmode = MYSQLI_STORE_RESULT) {
            $result = parent::query($query, $resultmode);
            if (!$result) {
                // or do whatever you wanna do when an error occurs
                throw new DBQueryException($this->error, $this->errno);
            }
            return $result;
        }
    }