本文实例分析了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
普通格式:
oop格式:
普通模式
,代码如下: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模式
,代码如下: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();
?>
希望本文所述对大家的PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/23002.html