如果行数大于0,则无法插入数据 [英] Insert data if number of rows is greater than 0 does not work

查看:102
本文介绍了如果行数大于0,则无法插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个PHP函数,它将把URL插入MySQL,但是只有那些直到执行时才存在的URL.我有一个问题,我的if条件只是被忽略了,所以一切都进入数据库,而忽略了条件.代码在这里:

I have this PHP function which will insert urls into MySQL, but only those which didn't exist until moment of executing. I have a problem, my if condition is simply ignored, so everything goes to database, ignoring the condition. Code is here:

function storeLink($url,$gathered_from) {

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

    if($row['COUNT(*)'] < 1);
    {
       echo "<font color='red'>".$row['COUNT(*)']."</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');
    }
}

我尝试使用MySQL count和PHP mysqli_num_rows,但仍然相同.无论if语句中的条件是什么,它都将忽略它.请帮助...

I tried with MySQL count and also with PHP mysqli_num_rows, but still the same. No matter what the condition in if statement is, it simply ignores it. Help please...

推荐答案

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');
    }
}

OR

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');
    }
}

这篇关于如果行数大于0,则无法插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆