Calgaryalberta Posted February 19, 2008 Share Posted February 19, 2008 Hey, I have a simple login script, and Im getting a Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\hosting\member\aiim\login\checklogin.php on line 254 Line 254 is this: $count=mysql_num_rows($result); Here is the entire script, I dont see anything wrong with it - Any Suggestions? <?php $host="****"; // Host name $username="***"; // Mysql username $password="*****"; // Mysql password $db_name="****"; // Database name $tbl_name="****"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from signup form $myusername=$_POST['myusername1']; $mypassword=$_POST['mypassword1']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername1"); session_register("mypassword1"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment Share on other sites More sharing options...
rameshfaj Posted February 19, 2008 Share Posted February 19, 2008 Thats the warning which means that the $result parameter provided in the mysql_numrows(..) did not get the value to count the no of rows.Check that the query is executed properly.It seems that the query did not gave any result. To remove the warning only put @ symbol infront of the called function:Ex @mysql_numrows()............ Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 19, 2008 Share Posted February 19, 2008 trie this way around.... <?php session_start(); $host="****"; // Host name $username="***"; // Mysql username $password="*****"; // Mysql password $db_name="****"; // Database name $tbl_name="****"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from signup form $myusername=$_POST['myusername1']; $mypassword=$_POST['mypassword1']; $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row // If result matched $myusername and $mypassword, table row must be 1 row if(mysql_num_row($result)==1){ while($x=mysql_fetch_assoc($result)){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION["myusername1"]=$x['myusername1']; $_SESSION["mypassword1=$x['mypassword1']; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } } ?> Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted February 19, 2008 Share Posted February 19, 2008 Try this: <?php $host=""; // Host name $username = ""; // Mysql username $password = ""; // Mysql password $db_name = ""; // Database name $tbl_name = ""; // Table name // Connect to server and select databse. @mysql_connect($host, $username, $password) or die("cannot connect"); @mysql_select_db($db_name) or die("cannot select DB"); // username and password sent from signup form $myusername = $_POST['myusername1']; $mypassword = $_POST['mypassword1']; $sql = "SELECT * FROM $tbl_name WHERE username = '$myusername' AND password = '$mypassword'"; $result = @mysql_query($sql); // Mysql_num_row is counting table row $count = mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count == 1) { // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername1"); session_register("mypassword1"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Also you might want to add some more security to that. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 19, 2008 Share Posted February 19, 2008 THIS SHOULD WORK ASWELL I THINK TRY IT.... <?php session_start(); //database connection $host="****"; // Host name $username="***"; // Mysql username $password="*****"; // Mysql password $db_name="****"; // Database name $tbl_name="****"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //name off submit button is submit....... if(isset($_POST['submit'])){ // username and password sent from signup form $myusername=$_POST['myusername1']; $mypassword=$_POST['mypassword1']; //select staement $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row if 1 row exist if(mysql_num_row($result)==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername1']=$myusername1; $_SESSION['mypassword1']=$mypassword1; header("location:login_success.php"); exit; }else{ // NOT A USER echo" SORRY WE DONT HAVE YOU AS A SIGNUP USER PLEASE REGISTER"; } } ?> Quote Link to comment Share on other sites More sharing options...
Calgaryalberta Posted February 19, 2008 Author Share Posted February 19, 2008 For the last script posted, and I added a @mysql_num_row($result); All parse errors are gone, but now it is saying "Wrong Username/Password" Problem is the username and password is right, I can't figure it out...Here is the new script <?php $host="****"; // Host name $username="****"; // Mysql username $password="****"; // Mysql password $db_name="***"; // Database name $tbl_name="****"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from signup form $myusername = $_POST['myusername1']; $mypassword = $_POST['mypassword1']; $sql = "SELECT * FROM $tbl_name WHERE username = '$myusername' AND password = '$mypassword'"; $result = @mysql_query($sql); // Mysql_num_row is counting table row $count = @mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count == 1) { // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername1"); session_register("mypassword1"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 19, 2008 Share Posted February 19, 2008 trie mine please......... Quote Link to comment Share on other sites More sharing options...
Calgaryalberta Posted February 19, 2008 Author Share Posted February 19, 2008 THIS SHOULD WORK ASWELL I THINK TRY IT.... Tried this code, got this error: Fatal error: Call to undefined function: mysql_num_row() in d:\hosting\member\aiim\login\checklogin.php on line 257 Line 257 Reads: if(mysql_num_row($result)==1){ <?php session_start(); //database connection $host="****"; // Host name $username="***"; // Mysql username $password="*****"; // Mysql password $db_name="****"; // Database name $tbl_name="****"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from signup form $myusername=$_POST['myusername1']; $mypassword=$_POST['mypassword1']; //select staement $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row if 1 row exist if(mysql_num_row($result)==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername1']=$myusername1; $_SESSION['mypassword1']=$mypassword1; header("location:login_success.php"); exit; }else{ // NOT A USER echo" SORRY WE DONT HAVE YOU AS A SIGNUP USER PLEASE REGISTER"; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 19, 2008 Share Posted February 19, 2008 code corrected make sure the form submit button is name="submit" <?php session_start(); //database connection $host="****"; // Host name $username="***"; // Mysql username $password="*****"; // Mysql password $db_name="****"; // Database name $tbl_name="****"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //name off submit button is submit....... if(isset($_POST['submit'])){ // username and password sent from signup form $myusername=$_POST['myusername1']; $mypassword=$_POST['mypassword1']; //select staement $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql)or die (mysql_error()); // Mysql_num_row is counting table row if 1 row exist if(mysql_num_rows($result)==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername1']=$myusername1; $_SESSION['mypassword1']=$mypassword1; header("location:login_success.php"); exit; }else{ // NOT A USER echo" SORRY WE DONT HAVE YOU AS A SIGNUP USER PLEASE REGISTER"; } } ?> Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted February 19, 2008 Share Posted February 19, 2008 No it's not the button that is named Submit if you add the isset($_POST['submit']). you must add a HIDDEN input field named submit with a value of TRUE Quote Link to comment Share on other sites More sharing options...
Calgaryalberta Posted February 19, 2008 Author Share Posted February 19, 2008 Tried the code above, seemed to work, except it didn't work. lol, the parse errors are gone, but with RedArrows code - the form submitted, but it said, 'Unknown column 'username' in Where Clause' Problem is, the field is true, in the database - the name of the column in the database is the same as it is on the script . So not sure where the 'Unknown Column 'Username' in Where Clause' is coming from. Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 You sure you have the right table? Quote Link to comment 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.