Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/199083-mysql_num_rows-question/
Share on other sites

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

$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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.