troubleshooting Question

mysql_affected_rows / mysql_num_rows

Avatar of psimation
psimationFlag for South Africa asked on
10 Comments1 Solution617 ViewsLast Modified:
HI Experts

OK, so I am a bit baffled now:

I know that when using a SELECT statement, I should use mysql_num_rows to determine if the resultset contains any rows, but here is what is baffling me:

It seems that while I was a bit absent minded, I produced the following code, and only saw it now when I re-examined some other code. What baffles me is that this code snippet is producing the expected results, even though it shouldn't!

Am I missing something? Maybe my code only "works" because I made two mistakes and somehow, in this case, two wrongs are in fact making a right?

function myDBValidationFunction($ajax)
$objResponse7 = new xajaxResponse(); 
$link = mysql_connect("myserver:3307", "user","pass");        
$username = mysql_real_escape_string($ajax[username]);
$password = mysql_real_escape_string($ajax[password]);  
$sql = "select * from users where username = \"$username\" and password = \"$password\" ";
$result = mysql_query($sql);     
if (mysql_affected_rows()){
$_SESSION[authed] = "true";
$myrow = mysql_fetch_assoc($result);
$_SESSION[name] = $myrow[name];
$_SESSION[uid] = $myrow[id];
$_SESSION[level] = $myrow[level];
} elseif(mysql_affected_rows() == "0") {
$_SESSION[authed] = "false";
return $objResponse7;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros