ฟังก์ชั่น mysql_affected_rows()
ความหมายของฟังก์ชั่นและการใช้งาน
การนับจำนวนแถวที่ได้รับผลกระทบจากการประมวลผลคำสั่งของ MySQL เช่น INSERT, UPDATE, DELETE ไปยังฐานข้อมูล ด้วยฟังก์ชั่น mysql_affected_rows()
รูปแบบการเขียน (Syntax)
int mysql_affected_rows ([ resource $link_identifier = NULL ] )
ตัวอย่างที่ 1
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
/* this should return the correct numbers of deleted records */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());
/* with a where clause that is never true, it should return 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n", mysql_affected_rows());
?>
Output :
Records deleted: 10
Records deleted: 0
ตัวอย่างที่ 2
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
/* Update records */
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Updated records: %d\n", mysql_affected_rows());
mysql_query("COMMIT");
?>
Output :
Updated Records: 10