MySQL空集错误行为

时间:2018-02-27 12:57:36

标签: php mysql mariadb

我正在使用MariaDb和PHP 7.我想知道为什么我一直都会得到错误的结果。这是我的MySQL架构和数据插入脚本 -

CREATE TABLE IF NOT EXISTS `BB_Orders`(
    `row_id` INT PRIMARY KEY AUTO_INCREMENT,
    `order_id` VARCHAR(255) NOT NULL DEFAULT ''
)
INSERT INTO `BB_Orders`(`row_id`, `order_id`) VALUES(1, '190061603-A')
INSERT INTO `BB_Orders`(`row_id`, `order_id`) VALUES(2, '190604470-A')
INSERT INTO `BB_Orders`(`row_id`, `order_id`) VALUES(3, '191206232-A')
INSERT INTO `BB_Orders`(`row_id`, `order_id`) VALUES(4, '191269277-A')

这是我的PHP函数定义 -

function ROWExists($Con, $TableName, $Condition){
    $RowID=0;
    $SelectStatement = "SELECT `row_id` FROM `".$TableName."`  WHERE ";
    foreach($Condition as $CKey=>$CValue){
        $SelectStatement.="TRIM(`".$CKey."`)=TRIM('".$CValue."') AND ";
    }
    $SelectStatement=rtrim($SelectStatement, ' AND ');
    $SelectStatement.=" LIMIT 1;";
    echo 'SELECT STATEMENT: '.$SelectStatement.PHP_EOL;
    echo $SelectStatement.PHP_EOL;
    $Resultset=mysqli_query($Con, $SelectStatement);
    if(!empty($Resultset)){
        if($Resultset->num_rows){
            while($Result=mysqli_fetch_assoc($Resultset)){
                if(!empty($Result)){
                    $RowID=$Result['row_id'];                       
                }
            }
        }   
    }
    return $RowID;
}

我使用 -

调用此函数
$RowID=ROWExists($Con, 'BB_Orders', array('order_id'=>'190061603-A'));
//returns the 'row_id' correct
$RowID=ROWExists($Con, 'BB_Orders', array('order_id'=>'190061603-Z'));
//returns the 'row_id' incorrect, this should have been 0, but not

0 个答案:

没有答案