定義和用法
這個mysqli_affected_rows()如果在 INSERT、UPDATE、REPLACE 或 DELETE 查詢之後調用,則函數返回受前一操作影響的行數。
在 select 語句之後使用時,此函數返回行數。
用法
mysqli_affected_rows($con)
參數
Sr.No | 參數及說明 |
---|---|
1 |
con(Mandatory) 這是一個表示與 MySQL 服務器的連接的對象。 |
返回值
PHP mysqli_affected_rows() 函數返回一個整數值,指示受前一個(SELECT、INSERT、UPDATE、REPLACE 或 DELETE)操作影響的行數。
如果前一個查詢有錯誤,則此函數返回-1.如果沒有受影響的行或前一個查詢/操作不是上述之一,則此函數返回0。
PHP版本
這個函數最初是在 PHP 版本 5 中引入的,適用於所有後續版本。
示例
以下示例演示了 mysqli_affected_rows() 函數的用法(程序風格) -
<?php
//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");
//Query to retrieve all the rows of employee table
mysqli_query($con, "SELECT * FROM employee");
//Effected rows
$rows = mysqli_affected_rows($con);
print("Number of affected rows:".$rows);
//Closing the connection
mysqli_close($con);
?>
這將產生以下結果 -
Number of affected rows:5
示例
在麵向對象的風格中,這個函數的語法是 $con -> affected_rows,其中,$con 是連接對象 -
<?php
//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");
//Query to retrieve all the rows of employee table
$con -> query("SELECT * FROM employee");
//Number of affected rows
$rows = $con -> affected_rows;
print("Number of affected rows:".$rows);
//Closing the connection
$con -> close();
?>
這將產生以下結果 -
Number of affected rows:5
示例
讓我們檢查這個函數的返回值,當它之前沒有(指定的)查詢時,當查詢有錯誤或它不影響任何行時 -
<?php
//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");
$rows1 = mysqli_affected_rows($con);
print("Rows Affected (no specified previous operations):".$rows1."\n");
//Query to retrieve all the rows of employee table
mysqli_query($con, "SELECT * FORM employee");
$rows2 = mysqli_affected_rows($con);
print("Rows Affected (when query has error):".$rows2."\n");
//Query to retrieve all the rows of employee table
mysqli_query($con, "SELECT FIRST_NAME FROM employee WHERE AGE <=19");
$rows3 = mysqli_affected_rows($con);
print("Rows Affected (when query does nothing):".$rows3."\n");
//Closing the connection
mysqli_close($con);
?>
這將產生以下結果 -
Rows Affected (no specified previous operations):0 Rows Affected (when query has error):-1 Rows Affected (when query does nothing):0
示例
以下示例演示了 mysqli_affected_rows 函數與 SELECT、UPDATE、INSERT 和 DELETE 查詢的用法 -
<?php
//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");
//Query to SELECT all the rows of the employee table
mysqli_query($con, "SELECT * FROM employee where INCOME > 8000");
print("Rows Affected by SELECT query:".mysqli_affected_rows($con)."\n");
//Query to UPDATE the rows of the employee table
mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000 where FIRST_NAME in ('Ramya', 'Trupthi', 'Sarmista')");
print("Rows Affected by UPDATE query:".mysqli_affected_rows($con)."\n");
//Query to INSERT a row into the employee table
mysqli_query($con, "INSERT INTO employee VALUES ('Archana', 'Mohonthy', 30, 'M', 13000, 106)");
print("Rows Affected by INSERT query:".mysqli_affected_rows($con)."\n");
//Query to DELETE rows of the employee table
mysqli_query($con, "DELETE FROM employee where AGE > 25");
print("Rows Affected by DELETE query:".mysqli_affected_rows($con)."\n");
//Closing the connection
mysqli_close($con);
?>
這將產生以下結果 -
Rows Affected by SELECT query:4 Rows Affected by UPDATE query:3 Rows Affected by INSERT query:1 Rows Affected by DELETE query:3
相關用法
- PHP mysqli_autocommit()用法及代碼示例
- PHP mysqli_get_server_info()用法及代碼示例
- PHP mysqli_data_seek()用法及代碼示例
- PHP mysqli_insert_id()用法及代碼示例
- PHP mysqli_fetch_assoc()用法及代碼示例
- PHP mysqli_connect_error()用法及代碼示例
- PHP mysqli_fetch_all()用法及代碼示例
- PHP mysqli_next_result()用法及代碼示例
- PHP mysqli_stmt_affected_rows()用法及代碼示例
- PHP mysqli_begin_transaction()用法及代碼示例
- PHP mysqli_real_escape_string()用法及代碼示例
- PHP mysqli_fetch_lengths()用法及代碼示例
- PHP mysqli_character_set_name()用法及代碼示例
- PHP mysqli_free_result()用法及代碼示例
- PHP mysqli_fetch_row()用法及代碼示例
- PHP mysqli_debug()用法及代碼示例
- PHP mysqli_commit()用法及代碼示例
- PHP mysqli_fetch_field()用法及代碼示例
- PHP mysqli_kill()用法及代碼示例
- PHP mysqli_thread_safe()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP mysqli_affected_rows() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。