简体   繁体   中英

Query always returns NULL

So the problem is the $check variable is always NULL, even though there is already a record in the database that match the SELECT statement I have written, it's still null. I know it's simple enough but i can't find the error. I already tried using empty, isNull, ====, but the problem is the $check is always null. Why it is always null?.

$sql = "SELECT * FROM linkedin_lookup WHERE "
              ."`given-name`='{$table['given_name']}' && "
              ."`family-name`='{$table['family_name']}' && "
              ."`location`='{$table['location']}' && "
              ."`industry`='{$table['industry']}' && "
              ."`headline`='{$table['headline']}'";
        $results = $db->query($sql);
        $check = $results->num_rows;
        if ($check):
            echo "Record already exist";
        else:
            $sql = "INSERT INTO linkedin_lookup (`given-name`,`family-name`,`location`,`industry`,`headline`,`date_search`) "
            ."VALUES "
            ."('{$table['given_name']}',"
            ."'{$table['family_name']}',"
            ."'{$table['location']}',"
            ."'{$table['industry']}',"
            ."'{$table['headline']}',"
            ."'{$now}')";
            $db->query($sql);
            echo "Succesfully inserted record(s)" . '<br />';
        endif;  

UPDATE MY CODE TO REFLECT INPUTS, but still it accepts duplicate. Is my SELECT STATEMENT wrong?. I put && right so it;s supposed to scan all fields if the same.

That's because you call num_rows on the result of the query (same for fetch_assoc ), NOT the array.

$results = $db->query($sql);
$check = $results->num_rows;
if( $check) {
    echo "Record already exists";
}
else {
    $sql = "INSERT.....";
}

Note that you could just try to insert it up front and check affected_rows - if it's zero then it faied to insert due to already existing. Note that you'll need a UNIQUE KEY on the table in the right fields.

If $results does infact contain something....and the query is infact working, which I dunno if it is....

You could just try this instead....

 $db->query($sql);
    $results = $db->fetch_assoc();

    if (count($results) > 0):
        //echo "Record already exist";
    else:

Ok now it works, i think the class $db is not mysqli, because we are using a framework that is developed by the company. So my codes now as follows and It works.

$sql = "SELECT * FROM linkedin_lookup WHERE "
          ."`given-name`='{$table['given_name']}' && "
          ."`family-name`='{$table['family_name']}' && "
          ."`location`='{$table['location']}' && "
          ."`industry`='{$table['industry']}' && "
          ."`headline`='{$table['headline']}'";
    $db->query($sql);
    $check = $db->fetch_array();
    if ($check):
        echo "Record already exist";
    else:
        $sql = "INSERT INTO linkedin_lookup (`given-name`,`family-name`,`location`,`industry`,`headline`,`date_search`) "
        ."VALUES "
        ."('{$table['given_name']}',"
        ."'{$table['family_name']}',"
        ."'{$table['location']}',"
        ."'{$table['industry']}',"
        ."'{$table['headline']}',"
        ."'{$now}')";
        $db->query($sql);
        echo "Succesfully inserted record(s)" . '<br />';
    endif;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM