首頁  >  文章  >  後端開發  >  對PHP函數mysqli_affected_rows的作用行數回傳值的分析

對PHP函數mysqli_affected_rows的作用行數回傳值的分析

巴扎黑
巴扎黑原創
2017-05-26 16:12:372174瀏覽

這篇文章主要介紹了PHP中mysqli_affected_rows作用行數回傳值,實例分析了普通模式與oop模式的用法,具有一定的參考借鑒價值,需要的朋友可以參考下

#本文實例分析了PHP中mysqli_affected_rows作用行數回傳值。分享給大家供大家參考。具體分析如下:

mysqli中關於update運算影響的行數可以有兩種回傳形式:

1. 傳回符合的行數
2. 傳回影響的行數

預設情況下mysqli_affected_rows傳回的值為影響的行數,如果我們需要傳回符合的行數,可以使用mysqli_real_connect函數進行資料庫連線的初始化,並在函數的flag參數位元加上:

MYSQLI_CLIENT_FOUND_ROWS return number of matched rows, not the number of affected rows

的普通格式:

#
int mysqli_affected_rows ( mysqli link )

#

class mysqli { 
int affected_rows 
}

#oop格式:

程式碼如下:

<?php 
$link = mysqli_connect("localhost", "my_user", "my_password", "world"); 
 
if (!$link) { 
printf("Can&#39;t connect to localhost. Error: %sn", mysqli_connect_error()); 
exit(); 
} 
 
/* Insert rows */ 
mysqli_query($link, "CREATE TABLE Language SELECT * from CountryLanguage"); 
printf("Affected rows (INSERT): %dn", mysqli_affected_rows($link)); 
 
mysqli_query($link, "ALTER TABLE Language ADD Status int default 0″); 
 
/* update rows */ 
mysqli_query($link, "UPDATE Language SET Status=1 WHERE Percentage > 50″); 
printf("Affected rows (UPDATE): %dn", mysqli_affected_rows($link)); 
 
/* delete rows */ 
mysqli_query($link, "DELETE FROM Language WHERE Percentage < 50″); 
printf("Affected rows (DELETE): %dn", mysqli_affected_rows($link)); 
 
/* select all rows */ 
$result = mysqli_query($link, "SELECT CountryCode FROM Language"); 
printf("Affected rows (SELECT): %dn", mysqli_affected_rows($link)); 
 
mysqli_free_result($result);
/* Delete table Language */ 
mysqli_query($link, "DROP TABLE Language"); 
 
/* close connection */ 
mysqli_close($link); 
?>

# 看下範例:

##普通模式,程式碼如下:

程式碼如下:

<?php 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 
 
/* check connection */ 
if (mysqli_connect_errno()) { 
printf("Connect failed: %sn", mysqli_connect_error()); 
exit(); 
} 
 
/* Insert rows */ 
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage"); 
printf("Affected rows (INSERT): %dn", $mysqli->affected_rows); 
 
$mysqli->query("ALTER TABLE Language ADD Status int default 0″); 
 
/* update rows */ 
$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50″); 
printf("Affected rows (UPDATE): %dn", $mysqli->affected_rows); 
 
/* delete rows */ 
$mysqli->query("DELETE FROM Language WHERE Percentage < 50″); 
printf("Affected rows (DELETE): %dn", $mysqli->affected_rows); 
 
/* select all rows */ 
$result = $mysqli->query("SELECT CountryCode FROM Language"); 
printf("Affected rows (SELECT): %dn", $mysqli->affected_rows); 
 
$result->close(); 
 
/* Delete table Language */ 
$mysqli->query("DROP TABLE Language"); 
 
/* close connection */ 
$mysqli->close(); 
?>


oop模式,程式碼如下:

程式碼如下:

rrreee

以上是對PHP函數mysqli_affected_rows的作用行數回傳值的分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn