Dark57 Posted April 20, 2010 Share Posted April 20, 2010 Ok so I'm using $sql = "SELECT COUNT(*) FROM mytable WHERE username = '$username'"; $result = mysql_query($sql); $username = stripslashes($username); $username = mysql_real_escape_string($username); $number=mysql_num_rows($result); if($number>0) { echo "Duplicate Username <br />"; $_SESSION['usernamefail']=1; } But I keep getting this error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\Silent Night Online\check_register.php on line 51 Line 51 refers to $number=mysql_num_rows($result); in this case. I'm not sure why it is returning a true/false statement if its supposed to be counting the matching rows of a username? Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/ Share on other sites More sharing options...
JAY6390 Posted April 20, 2010 Share Posted April 20, 2010 Chances are your query is failing. After the line $result = mysql_query($sql); put if(!$result) die(mysql_error().'<br />'.$sql); The query itself looks fine, so maybe a naming issue with the table name being a reserved word or no connection/db selected Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044934 Share on other sites More sharing options...
taquitosensei Posted April 20, 2010 Share Posted April 20, 2010 It's probably returning False because it couldn't connect, you didn't select a database, query is bad etc.. Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044935 Share on other sites More sharing options...
havenpets Posted April 20, 2010 Share Posted April 20, 2010 $username = stripslashes($username); $username = mysql_real_escape_string($username); $sql = "SELECT * FROM `mytable` WHERE username = '$username'"; $result = mysql_query($sql) or die(mysql_error()); $number = mysql_num_rows($result); if($number > 0) { echo "Duplicate Username <br />"; $_SESSION['usernamefail']=1; } Try that and let me know what happens. By the way, make sure you're cleaning (escaping) the variable before the MYSQL_QUERY() part, just to be safe. Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044936 Share on other sites More sharing options...
JAY6390 Posted April 20, 2010 Share Posted April 20, 2010 havenpets is right, you need to have the username above the $sql= ... line otherwise it won't be added into the query Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044938 Share on other sites More sharing options...
Dark57 Posted April 20, 2010 Author Share Posted April 20, 2010 Ah, I feel retarded now. I guess its a good thing I posted the code here so you could point out that to me too. I've been developing this on two different computers and this is a new database I'm working with. I forgot to run my create_db.php file before trying to use this file /facepalm Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044940 Share on other sites More sharing options...
JAY6390 Posted April 20, 2010 Share Posted April 20, 2010 These things happen sometimes Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044941 Share on other sites More sharing options...
Dark57 Posted April 20, 2010 Author Share Posted April 20, 2010 Yeah, especially when you're tired. Well I seem to have gotten it working now Thanks guys for the help, you guys always help me overcome my coding struggles. Quote Link to comment https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/#findComment-1044945 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.