Mysqli num行不起作用


Mysqli num rows not working

我有一个相当基本的脚本(我习惯了mysql,但我正在转向mysqli)

<?php
include("../includes/functions.php");
/////////////////////////////////////////////////////////////////////////
$debugMode = 1;
/////////////////////////////////////////////////////////////////////////
if (isset($_GET['u']))
{
    // database connection
    $c = mysqli_connect("localhost", "xxx", "xxx", "xxx");
    // initial query to check if the domain is already in the database
    $q = $c->query("SELECT * FROM `domains` WHERE `domain_name`='".trim($s[0])."'");
    $v = $q->fetch_assoc();
    $r = $q->num_rows;
    // check if the string exists in the database
    if ($r > 0)
    {
        // do not enter if greater than 0
    } else {        
        // vars
        $u = $_GET['u'];
        // DEBUG
        if ($debugMode)
        {
          $fp = fopen('u.txt', 'a');
          fwrite($fp, "$u" . "'n");
          fclose($fp);      
        }
        // do a split of "|"
        $s = explode("|", $u);
        // check alexa rank and update
        $alexa = alexa_rank($s[0]);
        // check connection
        if (mysqli_connect_errno())
        {
         echo mysqli_connect_error();
        } else {        
         // insert query if there is no errors
         $i = $c->query("INSERT INTO `domains` (`domain_id`,`domain_name`,`domain_pr`,`domain_alexa_rank`,`domain_moz_da`,`domain_moz_pa`,`domain_date`) VALUES ('','".$s[0]."','".$s[1]."','".$alexa."','".$s[2]."','".$s[3]."',NOW())"); 
        }   
    }   
} else {    
    header("Location: http://www.site.info/");
}
?>

它看起来相当简单,问题是计数部分不起作用,它仍在向数据库添加重复条目,我也尝试过:

mysqli_num_rows($q);

这似乎仍然不起作用,我是不是错过了一些简单的东西?

谢谢各位

您应该首先确保您的查询是正确的。

所以请这样调试您的代码;

$SQL = "SELECT * FROM `domains` WHERE domain_name`='".trim($s[0])."'";
echo "My Query: ". $SQL;
$q = $c->query($SQL);

我想你的$s变量有问题。如果您的sql查询是正确的,您可以检查$v变量是否有任何记录,如果$v变量是一个数组,并且具有值,则您有多个记录,如果没有,则可以传递给insert语句。

ahh谢谢!我发现我的top查询失败了,因为我在代码的后面使用了爆炸!谢谢大家。