数据库
首页 > 数据库> > PHP与sqlsrv_num_rows

PHP与sqlsrv_num_rows

作者:互联网

我试图执行SQL Server数据库的查询,着眼于多对多关系.我将需要行的表与关系表链接,并插入唯一的ID,在本例中为$guid.

该查询有时是功能性的.它会起作用,然后我只将$guid换成另一个,而即使我盯着桌子,并且它具有与该新$guid关联的值,它也会失败.

<?php
$guid = '{893C7BF8-E8C5-41D5-9C61-A72CF62DDBA8}';

// Connect to Database
$server = "#@#($#";     
$connectionSettings = array("Database"=>"db", "UID"=>"user", "PWD"=>"pword");
$connection = sqlsrv_connect($server, $connectionSettings);

if (!$connection){
    die("Failed Connection");
}

// Prepare and Execute Query
$sql = "SELECT * 
        FROM STREAMS.ATTACHMENTS a INNER JOIN STREAMS.RELATTACHMENTS ra
        ON a.ROWGUID = ra.ATTACHMENT_GUID
        WHERE ra.FEATURE_GUID = '" . $guid . "'";

$results = sqlsrv_query($connection, $sql);
$rowCount = sqlsrv_num_rows( $results );

if ($rowCount === false)
    echo "failure";
else
    echo $rowCount;

while($row = sqlsrv_fetch_array($results)){
    echo "loop";
}
?>

更奇怪的是,此代码的输出如下:

failurelooploop

因此,这意味着sqlsrv_num_rows命令不计算结果中的任何行…但是,这也意味着同一结果集有2行,因为while循环进行了两次.

有人可以解释这种时髦的行为吗?

谢谢.

解决方法:

我敢打赌,您会遇到某种错误:如果出现问题,sqlsrv_num_rows将返回FALSE.您可以通过以下方式获取错误输出:

// probably don't want this in production.
print_r( sqlsrv_errors());

我想原因与您的guid列有关,但我不确定.

标签:sql-server-2008,php,database
来源: https://codeday.me/bug/20191208/2088631.html