【问题标题】:Checking to see if record exist by RecordCount()通过 RecordCount() 检查记录是否存在
【发布时间】:2014-07-28 01:11:18
【问题描述】:

我在一个while循环中有这个函数,由于某种原因它说所有记录都是重复的。

我试过了:

$tst->RecordCount() === 1
$tst->RecordCount() == 1
$tst->RecordCount() = 1

$tst->RecordCount() === '1'
$tst->RecordCount() == '1'
$tst->RecordCount() = '1'

$tst->RecordCount() === "1"
$tst->RecordCount() == "1"
$tst->RecordCount() = "1"

但似乎没有一个有效

        $sql45 = "SELECT 
                count(*)
            FROM
                companies 
            WHERE 
                MC = " . $test;
    $tst = $conn->Execute($sql45);
    if ($tst === false) die("horrible death:" . $conn->ErrorMsg() . " SQL: " . $sql45); 

   if($tst->RecordCount() === 1){
        echo $test . "<br>";
        echo "Duplicate record <br>";
        continue;

    } 

这是我收到的

545481
Duplicate record 
45
Duplicate record 
11111

【问题讨论】:

  • var_dump($tst-&gt;RecordCount());

标签: mysql while-loop record-count


【解决方案1】:

你的记录数总是 1。你有

SELECT count(*) FROM companies WHERE MC = somevalue

这总是返回单行,也就是记录计数 1。也许它是您想要获得的计数的 VALUE?

【讨论】:

    【解决方案2】:

    您可以使用以下方法检查行是否存在:

    SELECT EXISTS(SELECT 1 FROM table1 WHERE ...)
    

    比count(*)效率高。希望对你有帮助。

    【讨论】:

    • 这仍然只返回一行(至少根据 sqlfiddle),这无济于事
    • Sql fiddle 仅支持涉及从 mysql 数据库模式中选择数据的查询。检查sqlfiddle.com/about.html
    猜你喜欢
    • 2011-02-20
    • 2015-01-21
    • 2012-08-13
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多