Hướng dẫn how can i get sql error in php? - làm cách nào tôi có thể gặp lỗi sql trong php?

❮ 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


(Php 4, Php 5)

mysql_error - Trả về văn bản của thông báo lỗi từ hoạt động MySQL trước đóReturns the text of the error message from previous MySQL operation

Sự mô tả

mysql_error (tài nguyên $link_identifier = null): Chuỗi(resource $link_identifier = NULL): string

Thông số

link_identifier

Kết nối MySQL. Nếu định danh liên kết không được chỉ định, liên kết cuối cùng được mở bởi mysql_connect () được giả định. Nếu không tìm thấy liên kết như vậy, nó sẽ cố gắng tạo một liên kết như thể mysql_connect () đã được gọi mà không có đối số. Nếu không tìm thấy kết nối hoặc thiết lập, lỗi cấp E_WARNING được tạo ra.mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

Trả về giá trị

Trả về văn bản lỗi từ hàm MySQL cuối cùng hoặc '' (chuỗi trống) nếu không xảy ra lỗi.

Ví dụ

Ví dụ #1 mysql_error () ví dụmysql_error() example

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

1049: Unknown database 'nonexistentdb'
1146: Table 'kossu.nonexistenttable' doesn't exist

Xem thêm

  • mysql_errno () - Trả về giá trị số của thông báo lỗi từ thao tác MySQL trước đó
  • »& NBSP; Mã lỗi MySQL

Aleczapka _at) gmx dot net ¶

18 năm trước

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.

Note. There is a bug in either documentation about error_reporting() or in mysql_error() function cause manual for mysql_error(), says:  "Errors coming back from the MySQL database backend no longer issue warnings." Which is not true.

Florian Sidler ¶

12 năm trước

Be aware that if you are using multiple MySQL connections you MUST support the link identifier to the mysql_error() function. Otherwise your error message will be blank.

$link_identifier0

Lâu đài Pendragon

13 năm trước

$link_identifier2

$link_identifier3

$link_identifier4

$link_identifier5

l dot poot tại twing dot nl ¶

16 năm trước

$link_identifier6

$link_identifier7

$link_identifier8

$link_identifier9

link_identifier0

link_identifier1

link_identifier2

link_identifier3

link_identifier4

Ẩn danh ¶

17 năm trước

link_identifier5

link_identifier6

link_identifier7

link_identifier8

link_identifier9

E_WARNING0

E_WARNING1

Olaf tại Amen-Online Dot de ¶

18 năm trước

E_WARNING3

E_WARNING4

Ẩn danh ¶

17 năm trước

E_WARNING6

E_WARNING7

E_WARNING8

Olaf tại Amen-Online Dot de ¶

21 năm trước

''0

''1

''2

Gianluigi_zanettini-megalab.it ¶

15 năm trước

''4

''5

''6

''7

''8

''9

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
0

Scott tại Rocketpack Dot Net

15 năm trước

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
2

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
3

Scott tại Rocketpack Dot Net

21 năm trước

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
5

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
6

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
7

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
8

<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");mysql_select_db("nonexistentdb"$link);
echo 
mysql_errno($link) . ": " mysql_error($link). "\n";mysql_select_db("kossu"$link);
mysql_query("SELECT * FROM nonexistenttable"$link);
echo 
mysql_errno($link) . ": " mysql_error($link) . "\n";
?>
9

Olaf tại Amen-Online Dot de ¶

21 năm trước

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.1

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.2

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.3

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.4

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.5

Gianluigi_zanettini-megalab.it ¶

15 năm trước

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.6

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.7

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.8

If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use  $php_errormsg. This Warning will be stored there.  You need to have track_errors set to true in your php.ini.9

Note. There is a bug in either documentation about error_reporting() or in mysql_error() function cause manual for mysql_error(), says:  "Errors coming back from the MySQL database backend no longer issue warnings." Which is not true.0

Note. There is a bug in either documentation about error_reporting() or in mysql_error() function cause manual for mysql_error(), says:  "Errors coming back from the MySQL database backend no longer issue warnings." Which is not true.1

Note. There is a bug in either documentation about error_reporting() or in mysql_error() function cause manual for mysql_error(), says:  "Errors coming back from the MySQL database backend no longer issue warnings." Which is not true.2

Làm thế nào tôi có thể nhận được lỗi cơ sở dữ liệu trong PHP?

PDO chỉ đơn giản là đặt mã lỗi để bạn kiểm tra bằng các phương thức PDO :: ErrorCode () và PDO :: ErrorInfo () trên cả đối tượng câu lệnh và cơ sở dữ liệu;Nếu lỗi xuất phát từ một cuộc gọi trên đối tượng câu lệnh, bạn sẽ gọi pDostatement :: ErrorCode () hoặc pDostatement :: ErrorInfo () phương thức trên đối tượng đó.PDO::errorCode() and PDO::errorInfo() methods on both the statement and database objects; if the error resulted from a call on a statement object, you would invoke the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object.

Làm thế nào các truy vấn SQL được chạy trong PHP?

Chúng ta có thể thực hiện truy vấn đối với cơ sở dữ liệu bằng phương thức php mysqli_query ().Cú pháp: Chúng ta có thể sử dụng phương thức mysqli_Query () theo hai cách: kiểu hướng đối tượng.Phong cách thủ tục.using the PHP mysqli_query() method. Syntax: We can use the mysqli_query( ) method in two ways: Object-oriented style. Procedural style.

Những chức năng nào được sử dụng để kiểm tra lỗi trong cơ sở dữ liệu MySQL?

Hàm mysqli_error () trả về mô tả về lỗi xảy ra trong cuộc gọi hàm mySQLI cuối cùng.mysqli_error() function returns the description of the error occurred during the last MySQLi function call.

Bạn có thể sử dụng SQL với PHP không?

Với PHP, bạn có thể kết nối và thao tác cơ sở dữ liệu.MySQL là hệ thống cơ sở dữ liệu phổ biến nhất được sử dụng với PHP.MySQL is the most popular database system used with PHP.