Hướng dẫn php mysqli execute error handling - xử lý lỗi thực thi php mysqli

Hoàn chỉnh

Bạn cần kiểm tra cả $mysqli$statement. Nếu chúng là sai, bạn cần đầu ra lần lượt là $mysqli->error hoặc $statement->error.

Hiệu quả

Đối với các tập lệnh đơn giản có thể chấm dứt, tôi sử dụng một lớp lót đơn giản kích hoạt lỗi PHP với thông báo. Đối với một ứng dụng phức tạp hơn, một hệ thống cảnh báo lỗi nên được kích hoạt thay thế, ví dụ bằng cách ném một ngoại lệ.

Ví dụ sử dụng 1: tập lệnh đơn giản

# This is in a simple command line script
$mysqli = new mysqli('localhost', 'buzUser', 'buzPassword');
$q = "UPDATE foo SET bar=1";
($statement = $mysqli->prepare($q)) or trigger_error($mysqli->error, E_USER_ERROR);
$statement->execute() or trigger_error($statement->error, E_USER_ERROR);

Ví dụ sử dụng 2: Ứng dụng

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}

<?php
/* Open a connection */
$link mysqli_connect("localhost""my_user""my_password""world");/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}
mysqli_query($link"CREATE TABLE myCountry LIKE Country");
mysqli_query($link"INSERT INTO myCountry SELECT * FROM Country");$query "SELECT Name, Code FROM myCountry ORDER BY Name";
if (
$stmt mysqli_prepare($link$query)) {/* drop table */
    
mysqli_query($link"DROP TABLE myCountry");/* execute query */
    
mysqli_stmt_execute($stmt);printf("Error: %s.\n"mysqli_stmt_error($stmt));/* close statement */
    
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>

Error: Table 'world.myCountry' doesn't exist.

mysqli_error

(Php 5, Php 7, Php 8)

MySQLI :: $ ERROR - mysqli_error - Trả về mô tả chuỗi của lỗi cuối cùng -- mysqli_errorReturns a string description of the last error

Sự mô tả

Phong cách hướng đối tượng

Phong cách thủ tục

mysqli_error (mysqli $mysql): Chuỗi(mysqli $mysql): string

Trả về giá trị

Một chuỗi mô tả lỗi. Một chuỗi trống nếu không xảy ra lỗi.

Ví dụ

Ví dụ #1 $ mysqli-> Ví dụ về lỗi

Phong cách hướng đối tượng

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
0

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
1

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
2

Phong cách thủ tục

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
3

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
1

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
5

mysqli_error (mysqli $mysql): Chuỗi

Error message: Unknown system variable 'a'

Trả về giá trị

  • Một chuỗi mô tả lỗi. Một chuỗi trống nếu không xảy ra lỗi.
  • Ví dụ
  • Ví dụ #1 $ mysqli-> Ví dụ về lỗi
  • Các ví dụ trên sẽ xuất ra:

Xem thêm

mysqli_connect_errno () - Trả về mã lỗi từ cuộc gọi kết nối cuối cùng

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
6

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
7

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
8

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}
9

mysqli_connect_error () - Trả về mô tả về lỗi kết nối cuối cùng

mysqli_connect_errno () - Trả về mã lỗi từ cuộc gọi kết nối cuối cùng

Error: Table 'world.myCountry' doesn't exist.
0

Error: Table 'world.myCountry' doesn't exist.
1

Error: Table 'world.myCountry' doesn't exist.
2

Error: Table 'world.myCountry' doesn't exist.
3

Error: Table 'world.myCountry' doesn't exist.
4

Error: Table 'world.myCountry' doesn't exist.
5

mysqli_connect_error () - Trả về mô tả về lỗi kết nối cuối cùng

mysqli_errno () - Trả về mã lỗi cho cuộc gọi chức năng gần đây nhất

Error: Table 'world.myCountry' doesn't exist.
6

Error: Table 'world.myCountry' doesn't exist.
7

Error: Table 'world.myCountry' doesn't exist.
8

Error: Table 'world.myCountry' doesn't exist.
9

Error message: Unknown system variable 'a'
0

mysqli_sqlstate () - Trả về lỗi SQLSTATE từ hoạt động MySQL trước đó

Thông tin tại Saunderswebsolutions dot com

Error message: Unknown system variable 'a'
1

16 năm trước

SE (at) Brainbits (DOT) NET ¶

Error message: Unknown system variable 'a'
2

Error message: Unknown system variable 'a'
3

Error message: Unknown system variable 'a'
4

Error: Table 'world.myCountry' doesn't exist.
5

Xem thêm

mysqli_connect_errno () - Trả về mã lỗi từ cuộc gọi kết nối cuối cùng

Error message: Unknown system variable 'a'
6

mysqli_connect_error () - Trả về mô tả về lỗi kết nối cuối cùng

mysqli_errno () - Trả về mã lỗi cho cuộc gọi chức năng gần đây nhất

Error message: Unknown system variable 'a'
7

Làm cách nào để xem lỗi mysqli?

Chỉ cần thêm hoặc chết (mysqli_error ($ db));Khi kết thúc truy vấn của bạn, điều này sẽ in lỗi MySQLI.add or die(mysqli_error($db)); at the end of your query, this will print the mysqli error.

Mysqli_connect_error () là gì?

Định nghĩa và sử dụng hàm Connect_error / mysqli_connect_error () trả về mô tả lỗi từ lỗi kết nối cuối cùng, nếu có.returns the error description from the last connection error, if any.

Việc sử dụng mysqli_error trong PHP là gì?

Hàm mysqli_error () được sử dụng để trả về lỗi trong cuộc gọi hàm MySQL gần đây nhất không thành công.Nếu có nhiều cuộc gọi chức năng MySQL, lỗi trong câu lệnh cuối cùng là câu được chỉ ra bởi hàm.to return the error in the most recent MySQL function call that failed. If there are multiple MySQL function calls, the error in the last statement is the one that is pointed out by the function.

Làm thế nào hiển thị thông báo lỗi MySQL trong PHP?

Thay vào đó, mô tả, sử dụng mysql_error () để truy xuất văn bản lỗi.Lưu ý rằng hàm này chỉ trả về văn bản lỗi từ hàm mySQL được thực thi gần đây nhất (không bao gồm mysql_error () và mysql_errno ()), vì vậy nếu bạn muốn sử dụng nó, hãy đảm bảo bạn kiểm tra giá trị trước khi gọi hàm MySQL khác.use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.