Hoàn chỉnh
Bạn cần kiểm tra cả $mysqli và $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_error — Returns 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 } 2Phong 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 } 5mysqli_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 } 9mysqli_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. 5mysqli_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' 0mysqli_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. 5Xem 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