Check whether a field value exists in a custom table when a webform is submitted I created a custom table named "test_customerid" for the use of select queries from the drupal database. The fields are naid (for national id), name and surname.

My aim is to display an error message if the user attempts to insert an National ID which is already present in the table. Otherwise, all information are added to the table "test_customerid". This must occur when a webform is submitted.

Here are my codes that I included in the webform.module of my drupal project (inside function webform_webform_submission_insert($node, $submission)): 

$accessnode=$node-&gt;nid;
if($accessnode==6)
{
    $dataSub=$submission-&gt;data[1]['value'];//submitted value of National ID

    $result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=&gt;$dataSub));
    if(!(empty($result)))//I also tried "if($result)"
    {
        drupal_set_message(t('The NID already exists.'), 'error'); 
    }
    else
    {

        db_insert('test_customerid')
        -&gt;fields(array(

        'naid' =&gt; $submission-&gt;data[1]['value'], 
        'name' =&gt; $submission-&gt;data[2]['value'],
        'surname' =&gt; $submission-&gt;data[3]['value'],
        ))
        -&gt;execute();
    }


The problem is: 
It displays the error message even if the user inputs a new National ID. I can't add new information in the table.

I even tried to convert the data into strings by using these before placing the variable into the insert query but it gives me the same error message:


$dataconvert=implode(",",$dataSub); 
$dataconvert=serialize($dataSub);


I want that the query to take the National ID value from the webform and check out if it exists in the table. Can you someone help me with this please?
Just replace 

$result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=&gt;$dataSub));


with 

$result=db_query("SELECT * FROM {test_customerid} WHERE naid = :naid",array(':naid'=&gt;$dataSub))-&gt;fetchAll(); 


and try, hope it will work