程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP中mysqli_affected_rows作用行數返回值

PHP中mysqli_affected_rows作用行數返回值

編輯:關於PHP編程

本文章來給各位同學介紹關於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 )
oop格式:
class mysqli {
int affected_rows
}
看下例子:
普通模式

 代碼如下 復制代碼

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

if (!$link) {
printf("Can'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);
?>


oop模式

 代碼如下 復制代碼

<?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();
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved