Mysql
 sql >> Base de données >  >> RDS >> Mysql

Insérer des données si le nombre de lignes est supérieur à 0 ne fonctionne pas

function storeLink($url,$gathered_from) {

    global $conn;   
    $querycheck = "SELECT COUNT(*) as CNT FROM test WHERE link = '$url'";
    $resultcheck = mysqli_query($conn, $querycheck);
    $row = mysqli_fetch_array($resultcheck, MYSQLI_ASSOC);

    if($row['CNT'] < 1) {
       echo "<font color='red'>".$row['CNT']."</font>";
       $url = mysqli_real_escape_string($conn,$url);
       $gathered_from = mysqli_real_escape_string($conn,$gathered_from);
       $query = "INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')";
       mysqli_query($conn,$query) or die('Error, insert query failed');
    }
}

OU

function storeLink($url,$gathered_from) {

    global $conn;   
    $querycheck = "SELECT link FROM test WHERE link = '$url'";
    $resultcheck = mysqli_query($conn, $querycheck);
    $row = mysqli_fetch_array($resultcheck, MYSQLI_ASSOC);

    if(mysqli_num_rows($resultcheck)==0) {
       echo "<font color='red'>".mysqli_num_rows($resultcheck)."</font>";
       $url = mysqli_real_escape_string($conn,$url);
       $gathered_from = mysqli_real_escape_string($conn,$gathered_from);
       $query = "INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')";
       mysqli_query($conn,$query) or die('Error, insert query failed');
    }
}