赞
踩
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
/* 本例返回被刪除記錄的準確數目 */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d/n", mysql_affected_rows());
/* 對於非真值的 WHERE 子句,應返回 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d/n", mysql_affected_rows());
/*一個插入失敗的SQL語句,因為返回值為-1,會輸出OK*/
mysql_query("insert into tableA(id)values('XXX')");
$affected_rows = mysql_affected_rows();
/*問題出在if這裡,執行成功則返回受影響的行的數目,如果最近一次查詢失敗的話,函數返回 -1。*/
if($affected_rows)
echo 'OK';
else
echo 'ERROR';
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。