-1

I'm having problems with a piece of code. I have a form that submits information to a MySQL database. I already saved product keys in database. I want to check whether key is present in database or not. I'm using following code:

    $namecheck = mysql_query("SELECT key FROM license_key WHERE key ='$userEnteredProductKey'");
    $count = mysql_num_rows($namecheck);        
    if($count)
        {
            die("FAILURE - <b>$product_name</b> has <b>NOT</b> been added because the reference number already exists.");
        }

if i run program it gives me error as:

 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in code.

Hope that makes sense, any help greatly appreciated.

Apb
  • 979
  • 1
  • 8
  • 25
  • 1
    This means your query returns nothing. Check the query. – Edwin Alex Mar 15 '13 at 11:28
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – deceze Mar 15 '13 at 11:29
  • So which query i have to use? – Apb Mar 15 '13 at 11:29
  • Something is missing in your query. check $userEnteredProductKey hold any value. or print your query and check – Sibiraj PR Mar 15 '13 at 11:31
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are probably (we can't see where `$userEnteredProductKey` comes from) **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 15 '13 at 11:32
  • it holds value because key is entered by user. I want to check that key whether it is present or not – Apb Mar 15 '13 at 11:33

3 Answers3

1

Try this :

$namecheck = mysql_query("SELECT `key` FROM license_key WHERE `key` ='$userEnteredProductKey'");

key is a reserved keyword in mysql, So better dont use it or wrap it in side `

Ref: What does the KEY keyword mean?

Community
  • 1
  • 1
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

Insted of using this if($count) try this one, if($count == 1)

Dev
  • 489
  • 5
  • 14
0

Use like this

$namecheck = mysql_query("SELECT `key` FROM license_key WHERE `key` ='".$userEnteredProductKey."'");