1

I have read through many other threads about this exact problem, but i for some reason can not solve my problem. Really need some help.

if (!$username||!$password||!$email)
    echo "Please fill out all fields"; 
 else
 {
    //encrypt password
    $password = md5($password);

    //check if username already taken
    $check = mysql_query("SELECT * FROM Test WHERE username = '$username'");
    if (mysql_num_rows($check)>=1)
       echo "Username already taken";

    else

It said

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in /Users.....

if (mysql_num_rows($check)>=1) This line..but when i run it in phpmyadmin, it returns results to me ok.

Please help

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Benyaman
  • 451
  • 2
  • 10
  • 25
  • [Little Bobby Tables](http://xkcd.com/327/) is expected to visit. – MisterBla Sep 12 '13 at 06:57
  • 1
    Don't use mysql_* functions in the first place.. they're deprecated and will be removed in one of the next PHP versions! – ciruvan Sep 12 '13 at 06:57
  • 1
    Oh goodness. Not only are `mysql_*` functions deprecated, but these queries are asking for a SQL injection. – Austin Brunkhorst Sep 12 '13 at 06:57
  • 1
    As well as not using `mysql_`... `md5` is a dated hashing algorithm, don't use it for passwords - look up [salts](https://www.google.co.uk/webhp?complete=0#complete=0&q=password+salt) and [blowfish](https://www.google.co.uk/webhp?complete=0#complete=0&q=blowfish+cipher) – naththedeveloper Sep 12 '13 at 06:57
  • Thanks, i just did that, i got an error back, No database selected. But the odd thing is, i can add the user infos into the database? – Benyaman Sep 12 '13 at 06:58
  • I know that i shouldnt use mysql_ and about SQL inject, i havent got through to that yet. Just want to resolve this before i move onto those – Benyaman Sep 12 '13 at 06:59
  • Did you actually connect to the database before doing the query? =P – Jite Sep 12 '13 at 07:01
  • I did, i connect to the database and i can add all the fields into the database but when i add users with the same exact username, it doesn't pick up that the names are the same. – Benyaman Sep 12 '13 at 07:02
  • But if the script says that you are not connected to the database, its likely you are not. Are you sure you connect to the database in the specific script that you run? – Jite Sep 12 '13 at 07:04
  • Yer i will spend some time to dig through it and see, but i think i found out what the reason is now, Thanks, but i will have to see if i can resolve it – Benyaman Sep 12 '13 at 07:11
  • @Clarklight Your `if/else` conditions hardly have any braces `{ }` so those conditions will fail. – Funk Forty Niner Sep 12 '13 at 12:46
  • Does this answer your question? [mysql\_fetch\_array()/mysql\_fetch\_assoc()/mysql\_fetch\_row()/mysql\_num\_rows etc... expects parameter 1 to be resource or result](https://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc) – Dharman Mar 30 '20 at 21:31

5 Answers5

2

Try to like this:

$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$result = mysql_query($query);  
$num_rows = mysql_num_rows($result); 
Guru
  • 621
  • 1
  • 4
  • 12
1

Change:

$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");

to

$check = mysql_query("SELECT * FROM Test WHERE username = '$username'") or die(mysql_error());

And you will see any potential errors that happens in the query.

Also, I would highly recomend using either PDO or Mysqli instead of mysql functions, as they are deprecated and will be removed in future php versions.

Jite
  • 5,761
  • 2
  • 23
  • 37
0

First You make sure that connection established correctly to the database.

Instead of writing query directly in

$check = mysql_query("SELECT * FROM Test WHERE username = '$username'");

Store the query in variable as

$query = "SELECT * FROM Test WHERE username = '".$username."'";

and execute it as

$check = mysql_query($query);

if you are still facing the issue,

echo the query as

echo $query;

and execute the query directly in phpmyadmin and you can find the issue.

I hope this helps.

Shafeeque
  • 2,039
  • 2
  • 13
  • 28
0

You can try like this

$sql= "SELECT * FROM Test WHERE username = '".$username."'";
$check = mysql_query($sql);

I think your $check returns null that is no user with this username and null can't be a parameter mysql_num_rows() function.

if($check)
{
    echo "Username already taken";
}
else
{
  echo "Username available";
  // do other actions
}
sharif2008
  • 2,716
  • 3
  • 20
  • 34
0

Ok, if anyone having the same issue, just add the variable within the if() statement two times, like so:

$Query = mysql_query($sql); 
IF($Query && mysql_num_rows($Query)> 0){ // continue with the code}

This should fix the issue.

Ali Abdul
  • 49
  • 2