
- PHP 教程
- PHP - 首頁
- PHP - 路線圖
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混用
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與I/O
- PHP - 數學函式
- PHP - Heredoc 和 Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期與時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 比較符
- PHP 控制語句
- PHP - 決策機制
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理CSV檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件的PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - Traits
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET & POST
- PHP - 檔案上傳
- PHP - Cookie
- PHP - Session
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 輸入過濾
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash 訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - Bug 除錯
- PHP - 針對C語言開發人員
- PHP - 針對Perl語言開發人員
- PHP - 框架
- PHP - 核心PHP與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 期望值
- PHP - use 語句
- PHP - 整數除法
- PHP - 已棄用的特性
- PHP - 已移除的擴充套件和SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
PHP mysqli_affected_rows() 函式
定義和用法
mysqli_affected_rows() 函式返回先前操作受影響的行數,如果在 INSERT、UPDATE、REPLACE 或 DELETE 查詢後呼叫。
在 select 語句之後使用時,此函式返回行數。
語法
mysqli_affected_rows($con)
引數
序號 | 引數及描述 |
---|---|
1 |
con(必填) 這是一個表示與 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_function_reference.htm
廣告