sqlsrv_has_rows()返回FALSE,但有记录[PHP]

时间:2019-03-27 05:00:01

标签: php sql-server sqlsrv

我不明白。 我对数据数组使用了循环foreach。 我在其中使用sqlsrv_query()然后返回有效的语句资源并始终返回一条记录, 但是某些记录表明sqlsrv_has_rows()返回false,尽管存在同一循环,但某些记录却返回true(大多数)。

我的代码:

foreach ($namedDataArray as $val) {

    $c++;
    $category = $val[$hcat];
    $mfg = $val[$hmfg];
    $sga = $val[$hsga];
    $grandtotal = $val[$hgt];
    $cat = trim($category,'1234567890. ');

    $values = "'$year', '$pe', '$month', '$cat', $mfg, $sga, $grandtotal, '$name', getdate()";
    $valuesedit = "years='$year', period='$pe', months='$month', category='$cat', mfg=$mfg, sga=$sga, gtt=$grandtotal, databy='$name', timein=getdate()";

    $res = getData($conn,'*','MScost',"years = '$year' and months='$month' and category='$cat'");
    $rows = sqlsrv_has_rows( $res );

    if ($rows === true){
        //$rp = editData($conn, "MScost", $valuesedit,"years = '$year' and months='$month' and category='$cat'");            
        echo "EditData </br>";
    }else {
        //$rp = addData($conn, "MScost", $values);
        echo "InsertData <br/>";
    }
}

我的函数getData:

function getData($conn,$field,$table,$condi){
    if($condi!=="") {
        $sql = "SELECT $field FROM $table WHERE $condi";
        $result = sqlsrv_query($conn, $sql);
        return $result;
    }else{
        $sql = "SELECT $field FROM $table";
        $result = sqlsrv_query($conn, $sql);            
        return $result;
    }
}

返回的示例资源:

EditData
EditData
InsertData
EditData
EditData
EditData
InsertData
EditData
EditData
EditData

请毫无疑问地帮助我的人解决此问题。谢谢。

0 个答案:

没有答案