我有这个PHP代码:
第一个查询查询-1工作正常.. 但 第二个query2也可以,但mysql_affected_rows()不会告诉我记录是否已更新,为什么?
感谢。
$uemail = 'driggg2@gmail.com';
$authkey = '2f4071bffda44aa30064364055687d13';
require_once ('./domainsiteconnect.php'); // Connect to the database.
if ( $uemail && $authkey)
{
echo "<br>--uemail: [$uemail] -- && -- [$authkey] --- ";
$query1 = "SELECT * FROM member WHERE email='$uemail' AND actkey='$authkey' AND status='pending' ";
$result1 = mysql_query ($query1);
$num1 = mysql_num_rows ($result1);
if ( $num1 == 1 ) // Exactly 1 rec found - Update member to verified.
{
echo '<br>+++ query1: successful +++>br>';
$status = 'verified';
$query2 = " UPDATE member SET status='$status' WHERE email='$uemail' "; //AND actkey='$authkey' ";
//- $query2 = " UPDATE member SET status='$status' WHERE email='$uemail' AND actkey='$authkey' ";
$result2 = mysql_query($query2);
$num2 = mysql_affected_rows($result2);
if ( $num2 ) // updated? - inform user
{
echo "<br>--$uemail updated to verified --";
}
else
echo "<br>--$uemail verfication [update] failed --";
$val1=mysql_errno();
$val2=mysql_error();
//--
echo '<br>['.$query2.']--<br>--['.$result2.']<br>val-1:'.$val1.'<br> val-2'.$val2;
}
} //-- eof. if auth/email flags ok ---
========================
结果输出为:
--uemail: [driggg2@gmail.com] -- && -- [2f4071bffda44aa30064364055687d13] ---
+++ query1: successful +++>br>
Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in /home/arif/public_html/amy/public/domainsite/verifyfirst.php on line 125
--driggg2@gmail.com verfication [update] failed --
[ UPDATE member SET status='verified' WHERE email='driggg2@gmail.com'; ]--
--[1]
val-1:0
val-2
我认为,警告是因为query2没有返回mysqL_affected_rows()所需的资源ID。
答案 0 :(得分:2)
基本上,状态是mysql_affected_rows()不接受任何参数。在更新的上下文中将输出受影响的行。因此,您无需传递$ result2。
而不是
$ num2 = mysql_affected_rows($ result2);
使用
$ num2 = mysql_affected_rows();
答案 1 :(得分:0)
确实,将mysql_connect的结果放在mysql_affected_rows中以消除错误消息并获得实际受影响的行数。