Zoroaster Posted August 16, 2009 Share Posted August 16, 2009 Hey, I'm new here - I hope I'm posting this at the right place, and if I'm not I'm sorry. Please bare with me, I'm a PHP newbie. Anyway... I've checked over the code over and over and I can't seem to find any errors, so I hope you can help me. The error message i get is this: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/XXXXXXX/public_html/checklogin.php on line 18 the code is below: <? $host = "localhost"; $username = "XXXX"; $password = "XXXX"; $db_name = "XXXXXXXX_test1"; $tbl_name = "Login"; mysql_connect($host, $username, $password) or die("Failed to connect to mysql!"); mysql_select_db($db_name) or die ("Failed to connect to mysql!"); $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170535-solved-php-newbie-cant-understand-error-message/ Share on other sites More sharing options...
sKunKbad Posted August 16, 2009 Share Posted August 16, 2009 The error means that there was nothing returned from the SQL query. Either this sql query is not correct, or you need to test for the presence of 0. Quote Link to comment https://forums.phpfreaks.com/topic/170535-solved-php-newbie-cant-understand-error-message/#findComment-899528 Share on other sites More sharing options...
ignace Posted August 16, 2009 Share Posted August 16, 2009 $count = mysql_num_rows($result); instead write: if ($result && ($count = mysql_num_rows($result))) { .. } Quote Link to comment https://forums.phpfreaks.com/topic/170535-solved-php-newbie-cant-understand-error-message/#findComment-899555 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2009 Share Posted August 16, 2009 That particular error means that your query failed to even execute and a FALSE value was returned instead of a result resource (which is why the error mentions that the - supplied argument is not a valid MySQL result resource.) A query that execuits but matches zero rows is a successful query and returns a valid result resource and does not produce php errors when it is accessed. Your query looks correct. That either means there is something in the data from the form that you put into the query that is breaking the query syntax (you need to use mysql_real_escape_string() on the string values put into the query to prevent sql injection and to prevent sql special characters that might be in the data from breaking the query syntax) or your table name/columns have some problem. echo $sql; so that you can see exactly what is in it and echo mysql_error(); right after the mysql_query() statement to see what if any errors are being produced. Quote Link to comment https://forums.phpfreaks.com/topic/170535-solved-php-newbie-cant-understand-error-message/#findComment-899570 Share on other sites More sharing options...
Zoroaster Posted August 16, 2009 Author Share Posted August 16, 2009 Thanks guys. Made me realise that there was something wrong with my database and there was. I simply put user and pass instead of username and password. So simple but yet so annoying! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170535-solved-php-newbie-cant-understand-error-message/#findComment-899588 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.