dqtdz08206 2013-11-14 11:32
浏览 20
已采纳

如果行数大于0则插入数据不起作用

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

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...

  • 写回答

3条回答 默认 最新

  • doudou5023 2013-11-14 11:35
    关注
    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');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料