Hướng dẫn mysqli_error not returning anything - mysqli_error không trả về bất cứ thứ gì

Tôi có một tập lệnh PHP được cho là chèn một hàng mới vào cơ sở dữ liệu từ xa. Một cái gì đó không hoàn toàn hoạt động như bình thường, nhưng khi tôi cố gắng gỡ lỗi với

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";
2, nó không trả lại bất cứ điều gì. Mã như sau:

$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Unable to Connect to '$dbhost'");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "INSERT INTO #DBname.TableName# (Section,Gender,WinningTeam,LosingTeam,FixtureD,FixtureT,Venue,Court,Texts,SetsWon,SetsLost,Winner_Score,Loser_Score) VALUES ($Section,$Gender,$WinningTeam,$LosingTeam,$FixtureD,$FixtureT,$Venue,$Court,$Texts,$SetsWon,$SetsLost,$Winner_Score,$Loser_Score)";              

$result = mysqli_query($connect, $query) or die (mysqli_error());

Thông báo lỗi cho biết

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";
2 mong đợi chính xác 1 tham số, 0 đã được đưa ra. Tuy nhiên, khi tôi thay thế nó bằng
$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";
4, tôi nhận được lỗi 404 - tập lệnh PHP không tìm thấy trên máy chủ.

Khá chắc chắn tôi đang thiếu một cái gì đó rõ ràng, có ai có thể giúp đỡ không?

Trân trọng

I am using the following code to delete a record. It works fine, but MySQL always returns a blank error message. PHP returns "Error: " instead of "1 Record Dropped".

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";

When I use the same PHP code and change the SQL to a SELECT or INSERT INTO command instead of a DELETE command, I receive the correct response from PHP.

asked Jul 23, 2014 at 5:29Jul 23, 2014 at 5:29

1

Doh! @jynus was exactly correct. Here is the fix:

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
$sql="DELETE FROM Events WHERE EventID='$eventid' LIMIT 1";
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo mysqli_affected_rows() . " Record(s) Dropped";

answered Jul 23, 2014 at 17:28Jul 23, 2014 at 17:28

JustinJustinJustin

312 silver badges6 bronze badges2 silver badges6 bronze badges

❮ Php mysqli tham khảo

Nội dung chính ShowShow

  • Ví dụ - Phong cách định hướng đối tượng
  • Định nghĩa và cách sử dụng
  • Giá trị tham số
  • Chi tiết kỹ thuật
  • Ví dụ - Phong cách định hướng thủ tục
  • Định nghĩa và cách sử dụng
  • Giá trị tham số
  • Trả về giá trị
  • Phiên bản PHP
  • Mysqli_error là gì?
  • Tại sao mysqli_query được sử dụng trong PHP?
  • Việc sử dụng mysqli_num_rows là gì?
  • Làm thế nào tôi có thể thấy lỗi SQL trong PHP?

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

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

Giá trị tham số

Chi tiết kỹ thuật
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

Ví dụ - Phong cách định hướng thủ tục
if (!$mysqli -> query("INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
  echo("Error description: " . $mysqli -> error);
}

Thông số
?>

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ó:


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

Giá trị tham số


Chi tiết kỹ thuật

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

Thông số

Giá trị tham số

Chi tiết kỹ thuậtVí dụ - Phong cách định hướng thủ tục
Thông số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ó:

Chi tiết kỹ thuật

Ví dụ - Phong cách định hướng thủ tụcThông số
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");

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

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

Giá trị tham số

Chi tiết kỹ thuật
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}

Ví dụ - Phong cách định hướng thủ tục
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
  echo("Error description: " . mysqli_error($con));
}

Thông số
?>



❮ Php mysqli tham khảo




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

Giá trị tham sốmysqli_error() function returns the description of the error occurred during the last MySQLi function call.

Chi tiết kỹ thuật

mysqli_error($con)

Giá trị tham số

Sr.NoChi tiết kỹ thuật
1

con(Mandatory)

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

Trả về giá trị

Hàm php mysqli_error () trả về giá trị chuỗi biểu thị mô tả lỗi từ lệnh gọi hàm mySQLI cuối cùng. Nếu không có lỗi, chức năng này sẽ trả về một chuỗi trống.

Phiên bản PHP

Chức năng này lần đầu tiên được giới thiệu trong Php phiên bản 5 và hoạt động trong tất cả các phiên bản sau.

Thí dụ

Ví dụ sau đây cho thấy việc sử dụng hàm mysqli_error () (theo kiểu thủ tục) -

Điều này sẽ tạo ra kết quả sau -

Error Occurred: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORM employee' at line 1

Thí dụ

Ví dụ sau đây cho thấy việc sử dụng hàm mysqli_error () (theo kiểu thủ tục) -

 query("SELECT * FROM wrong_table_name");

   //Error 
   $error = $con ->error;
   print("Error Occurred: ".$error);

   //Closing the connection
   $con -> close();
?>

Điều này sẽ tạo ra kết quả sau -

Error Occurred: Table 'mydb.wrong_table_name' doesn't exist

Thí dụ

Ví dụ sau đây cho thấy việc sử dụng hàm mysqli_error () (theo kiểu thủ tục) -

Điều này sẽ tạo ra kết quả sau -

Errors in the SELECT query:
Errors in the UPDATE query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*)' at line 1
Errors in the INSERT query: Unknown column 'Archana' in 'field list'

Thí dụ

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";
0

Điều này sẽ tạo ra kết quả sau -

$eventid = intval(mysqli_real_escape_string($dbconnect, $_POST['eventid']));
mysqli_query($dbconnect,"DELETE FROM Events WHERE EventID='$eventid' LIMIT 1");
if (!mysqli_query($dbconnect,$sql)) {
  die('Error: ' . mysqli_error($dbconnect));
}
echo "1 Record Dropped";
1

Trong kiểu định hướng đối tượng, cú pháp của hàm này là $ con -> lỗi. Sau đây là ví dụ về chức năng này theo kiểu hướng đối tượng -

Mysqli_error là gì?

Định nghĩa và 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ó.returns the last error description for the most recent function call, if any.returns the last error description for the most recent function call, if any.

Tại sao mysqli_query được sử dụng trong PHP?

Định nghĩa và cách sử dụng.Hàm mySQLI_Query () chấp nhận giá trị chuỗi đại diện cho một truy vấn là một trong các tham số và, thực thi/thực hiện truy vấn đã cho trên cơ sở dữ liệu.accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database.accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database.

Việc sử dụng mysqli_num_rows là gì?

Hàm mysqli_num_rows () trả về số lượng hàng trong một tập kết quả.returns the number of rows in a result set.returns the number of rows in a result set.

Làm thế nào tôi có thể thấy lỗi SQL trong PHP?

Bạn chỉ có thể sử dụng: $ this-> db_link-> lỗi để nhận thông báo lỗi cuối cùng.Đối với tất cả các lỗi, hãy sử dụng $ this-> db_link-> error_list.$this->db_link->error to get the last error message. For all errors use $this->db_link->error_list .$this->db_link->error to get the last error message. For all errors use $this->db_link->error_list .