Hướng dẫn how do i find mysqli query error? - làm cách nào để tìm lỗi truy vấn mysqli?

❮ Php mysqli tham khảo

Ví dụ - Phong cách định hướng đối tượng

Trả về mô tả lỗi cuối cùng cho cuộc gọi chức năng gần đây nhất, nếu có:

$mysqli = new mysqli("localhost","my_user","my_password","my_db");

if ($ mysqli -> connect_errno) {& nbsp; Echo "Không thể kết nối với MySQL:". $ mysqli -> Connect_error; & nbsp; lối ra(); }
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// thực hiện truy vấn, kiểm tra lỗi (! $ Mysqli -> truy vấn ("chèn vào giá trị của người (firstName) ('glenn')")) {& nbsp; echo ("Mô tả lỗi:". $ mysqli -> lỗi); }
if (!$mysqli -> query("INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
  echo("Error description: " . $mysqli -> error);
}

$ mysqli -> đóng ();?>
?>


Nhìn vào ví dụ về phong cách thủ tục ở phía dưới.


Định nghĩa và cách sử dụng

Hàm lỗi / mysqli_error () trả về mô tả lỗi cuối cùng cho cuộc gọi chức năng gần đây nhất, nếu có.


Cú pháp

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

Phong cách thủ tục:

Giá trị tham số

Tham sốSự mô tả
sự liên quanYêu cầu. Chỉ định kết nối MySQL để sử dụng

Chi tiết kỹ thuật

Giá trị trở lại:Trả về một chuỗi với mô tả lỗi. "" Nếu không xảy ra lỗi
Phiên bản PHP:5+

Ví dụ - Phong cách định hướng thủ tục

Trả về mô tả lỗi cuối cùng cho cuộc gọi chức năng gần đây nhất, nếu có:

$con=mysqli_connect("localhost","my_user","my_password","my_db");

if ($ mysqli -> connect_errno) {& nbsp; Echo "Không thể kết nối với MySQL:". $ mysqli -> Connect_error; & nbsp; lối ra(); }
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}

// thực hiện truy vấn, kiểm tra lỗi (! $ Mysqli -> truy vấn ("chèn vào giá trị của người (firstName) ('glenn')")) {& nbsp; echo ("Mô tả lỗi:". $ mysqli -> lỗi); }
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
  echo("Error description: " . mysqli_error($con));
}

$ mysqli -> đóng ();?>
?>



❮ Php mysqli tham khảo


mysqli_error

// Truy vấn trả về 0 hàng !.

} khác {. -- mysqli_errorReturns a string description of the last error

// Truy vấn trả về ít nhất một hàng ..

Hàm MySQLI nào trả về mã lỗi cuối cùng từ lỗi kết nối cuối cùng?

Hàm lỗi / mysqli_error () trả về mô tả lỗi cuối cùng cho cuộc gọi chức năng gần đây nhất, nếu có.

Giá trị trả về của hàm mysqli_query () sẽ là gì?(mysqli $mysql): string

Trả về giá trị ¶ Trả về sai khi thất bại. Đối với các truy vấn thành công tạo ra một tập kết quả, chẳng hạn như chọn, hiển thị, mô tả hoặc giải thích, mysqli_query () sẽ trả về đối tượng mysqli_result. Đối với các truy vấn thành công khác, mysqli_query () sẽ trả về đúng.

(Php 5, Php 7, Php 8)

Ví dụ

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

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

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}

if (!

$mysqli->query("SET a=1")) {
    
printf("Error message: %s\n"$mysqli->error);
}
/* close connection */
$mysqli->close();
?>

Phong cách thủ tục

$link mysqli_connect("localhost""my_user""my_password""world");/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

if (!

mysqli_query($link"SET a=1")) {
    
printf("Error message: %s\n"mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>

Các ví dụ trên sẽ xuất ra:

Error message: Unknown system variable 'a'

Xem thêm

  • mysqli_connect_errno () - Trả về mã lỗi từ cuộc gọi kết nối cuối cùng
  • 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
  • mysqli_sqlstate () - Trả về lỗi SQLSTATE từ hoạt động MySQL trước đó

Thông tin tại Saunderswebsolutions dot com

16 năm trước

The mysqli_sql_exception class is not available to PHP 5.05

I used this code to catch errors
$query = "SELECT XXname FROM customer_table ";
$res = $mysqli->query($query);

$mysql0

$mysql1

SE (at) Brainbits (DOT) NET ¶

16 năm trước

$mysql2

$mysql3

$mysql4

$mysql5

$mysql6

$mysql7

SE (at) Brainbits (DOT) NET ¶

callforeach tại gmail dot com

$mysql8

$mysql9

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
0

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
1

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
2

7 năm trước

Abderrahmanekaddour dot aissat tại gmail dot com ¶

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
3

1 tháng trước

asmith26 tại Littlesvr dot ca ¶

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
4

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
5

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
6

$mysql7

Thông tin tại Saunderswebsolutions dot com

16 năm trước

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
8

SE (at) Brainbits (DOT) NET ¶

callforeach tại gmail dot com

$mysqli = new mysqli("localhost""my_user""my_password""world");/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
9

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.

Làm thế nào để tôi biết nếu mysqli truy vấn thành công?

Làm thế nào để kiểm tra xem một truy vấn MySQLI có thành công trong câu trả lời mã PHP hay không..
8 năm trước
// peform một truy vấn ..
$ query = "Chọn`*`từ người dùng" ;.
$ results = mysqli_query ($ databaseConnection, $ query) ;.
if (mysqli_num_rows ($ results) == 0) {.
// Truy vấn trả về 0 hàng !.
} khác {.
// Truy vấn trả về ít nhất một hàng ..

Hàm MySQLI nào trả về mã lỗi cuối cùng từ lỗi kết nối cuối cùng?

Hàm lỗi / mysqli_error () trả về mô tả lỗi cuối cùng cho cuộc gọi chức năng gần đây nhất, nếu có.error / mysqli_error() function returns the last error description for the most recent function call, if any.

Giá trị trả về của hàm mysqli_query () sẽ là gì?

Trả về giá trị ¶ Trả về sai khi thất bại.Đối với các truy vấn thành công tạo ra một tập kết quả, chẳng hạn như chọn, hiển thị, mô tả hoặc giải thích, mysqli_query () sẽ trả về đối tượng mysqli_result.Đối với các truy vấn thành công khác, mysqli_query () sẽ trả về đúng.Returns false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN , mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true .