Acute Chaos Posted July 7, 2011 Share Posted July 7, 2011 If the fname, lname and id the user fills into the form match what is in the database I want this to go on to the next bit of code but... even though they are a match I am getting the error message that 'this doesn't work try again'. if($login != '') { $qry = "SELECT fname, lname, login FROM user WHERE fname='$fname' AND lname='$lname' AND login='$login'"; $result = mysql_query($qry); if($result) { if(mysql_num_rows($result) != 3) { $errmsg_arr[] = 'this doesn't work try again'; $errflag = true; } @mysql_free_result($result); } else { die("Query failed"); } } Is my !=3 wrong? I figure this should return 3 rows, no? So if the result doesn't equal 3 then something went wrong and the code shouldn't continue. That was my logic. Where have I gone wrong!!?? Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/ Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2011 Share Posted July 7, 2011 What result is echoed when you echo mysql_num_rows($result)? I'm guessing it's going to be either 0 or 1. mysql_num_rows() returns the number of records the query retrieves, not the number of individual fields. Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/#findComment-1239602 Share on other sites More sharing options...
Acute Chaos Posted July 7, 2011 Author Share Posted July 7, 2011 God help me, I can't even get the syntax right today to echo out the results. I'm such a noob. Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/#findComment-1239605 Share on other sites More sharing options...
Acute Chaos Posted July 7, 2011 Author Share Posted July 7, 2011 Ok but regardless of my lack of coding knowledge here (which is extensive - the lack that is...) If it returns only the records then if the three fields match up successfully it should return 1 record, right? So if I change this to == 1 - that should work right? thanks Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/#findComment-1239607 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2011 Share Posted July 7, 2011 If you expect one record, no more and no less (like when checking a user's login credentials), then yes. Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/#findComment-1239609 Share on other sites More sharing options...
Acute Chaos Posted July 7, 2011 Author Share Posted July 7, 2011 I might have to rethink this whole thing... I am reallllly new to this and I'm trying to put together this going through tutorials and examples I can find online. I am starting with three bits of information about a member in the database to begin with before any registration has begun as I am creating something for a specific group. When they come to register at the site I was going to use that information to verify who they were before allowing them to continue with the registration. So this part of the code was to authenticate the user before updating the database with a couple other require bits of info in the registration form. Is there an obvious thing I'm missing here? tx Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/#findComment-1239616 Share on other sites More sharing options...
Acute Chaos Posted July 7, 2011 Author Share Posted July 7, 2011 OK! so I am good and past that hurdle. The query works but when I get to the next step where the extra bits of info I am gathering are to be inserted into the database I get "Query failed here2". $qry = "INSERT INTO user (email, password) VALUES ('$email', '".md5($_POST['password'])."')"; $result = @mysql_query($qry); if($result) { header("location: register-success.php"); exit(); }else { die("Query failed here2"); } Other than the fact I haven't separated the login in info and the other info into two separate tables yet... Should this not work? Thanks!! Both sets of code together: if($login != '') { $qry = "SELECT fname, lname, login FROM user WHERE fname='$fname' AND lname='$lname' AND login='$login'"; $result = mysql_query($qry); if($result) { if(mysql_num_rows($result) != 1) { $errmsg_arr[] = 'this doesn't work try again'; $errflag = true; } @mysql_free_result($result); } else { die("Query failed here1"); } } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: register-form.php"); exit(); } $qry = "INSERT INTO user (email, password) VALUES ('$email', '".md5($_POST['password'])."')"; $result = @mysql_query($qry); if($result) { header("location: register-success.php"); exit(); }else { die("Query failed here2"); } Thanks for any help. If I can get past this... I'm almost there! Quote Link to comment https://forums.phpfreaks.com/topic/241323-query-failed-num_rows/#findComment-1239661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.