2013-03-15 105 views
-1

我遇到了一段代码的问题。我有一个表单将信息提交给MySQL数据库。我已经将产品密钥保存在数据库中。我想检查数据库中是否存在密钥。我使用下面的代码:检查产品密钥是否已经存在于数据库中?

$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."); 
     } 

如果我运行程序它给我的错误是:

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

希望是有道理的,任何帮助,不胜感激。

+1

这意味着您的查询返回任何内容。检查查询。的 – 2013-03-15 11:28:47

+0

可能重复的[MySQL的\ _fetch \ _array()预计参数1是资源,在布尔选择给定](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to -be-resource-boolean-given-in-select) – deceze 2013-03-15 11:29:28

+0

那么我必须使用哪个查询? – Apb 2013-03-15 11:29:54

回答

0

insted的使用这个 如果($ count) 试试这个, if($ count == 1)

1

试试这个:

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

key是MySQL中的保留关键字,所以最好不使用它或侧面`把它包

编号:What does the KEY keyword mean?

0

使用l IKE在此

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