简体   繁体   English

fetch_array中的sqlsvr_fetch_array

[英]sqlsvr_fetch_array within fetch_array

So I have a PHP script I am convering from MSSQL to SQLSRV and I am having some difficulty. 所以我有一个PHP脚本,正在从MSSQL转换到SQLSRV,并且遇到了一些困难。

I have this 我有这个

 while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{

$time = time() - strtotime('today');

$row2sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."'";
$row3sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."' AND strDate>'".$row[strDate]."' AND mnyAmount<'0'";
$row4sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."' AND strDate>'".$row[strDate]."' AND mnyAmount>'0'";

$row2 = sqlsrv_fetch_array($row2sql, SQLSRV_FETCH_ASSOC);
$row3 = sqlsrv_fetch_array($row3sql, SQLSRV_FETCH_ASSOC);
$row4 = sqlsrv_fetch_array($row4sql, SQLSRV_FETCH_ASSOC);

Which essentially pulls data based on the search and then cross references their data per row. 它实质上是根据搜索来提取数据,然后每行交叉引用其数据。 In MySQL this wouldn't be a problem - but seem to be having difficult here? 在MySQL中,这不是问题-但在这里似乎很难?

Basically, if I search "Smith" it will bring up all users with Smith in their name...(for example three rows) then I want it to get each of their balances respectively. 基本上,如果我搜索“ Smith”,它将显示所有以Smith为名的用户(例如三行),那么我希望它分别获取他们的每个余额。

Can anyone help please? 有人可以帮忙吗?

Thank you. 谢谢。

You're missing the call to sqlsrv_query for each of your queries. 您缺少每个查询的sqlsrv_query调用。

ie you should have something like this for each of your sub queries (see example 1 http://www.php.net/manual/en/function.sqlsrv-fetch-array.php ): 也就是说,每个子查询都应具有类似的内容(请参见示例1 http://www.php.net/manual/zh/function.sqlsrv-fetch-array.php ):

$sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    echo $row['LastName'].", ".$row['FirstName']."<br />";
}

sqlsrv_free_stmt( $stmt);

Given that you have used both MySQL and SQLServer, it is probably worth looking at using PDO ( http://uk3.php.net/pdo ). 鉴于您同时使用了MySQL和SQLServer,可能值得一看使用PDO( http://uk3.php.net/pdo )。 This will allow you to alter configuration instead of having to edit code should you need to change db server. 如果需要更改数据库服务器,这将允许您更改配置,而不必编辑代码。 It also makes code consistent. 它还使代码一致。

This would be the fixed code. 这将是固定代码。 The first parameter of sqlsrv_fetch_array has to be a resource, you're giving a string here. sqlsrv_fetch_array的第一个参数必须是资源,您在此处提供了一个字符串。 You have to execute the query first before "unzipping" it. 您必须先执行查询,然后才能“解压缩”它。

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{

$time = time() - strtotime('today');

$row2sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."'";
$row3sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."' AND strDate>'".$row[strDate]."' AND mnyAmount<'0'";
$row4sql = "SELECT SUM(mnyAmount) AS 'Total' FROM dbo.tblTransactions WHERE intPerson='".$row[intPerson]."' AND strDate>'".$row[strDate]."' AND mnyAmount>'0'";
//$connection is the connection variable you get from opening a connection to the database
$result2 = sqlsrv_query($connection, $row2sql);
$result3 = sqlsrv_query($connection, $row3sql);
$result4 = sqlsrv_query($connection, $row4sql);
$row2 = sqlsrv_fetch_array($result2, SQLSRV_FETCH_ASSOC);
$row3 = sqlsrv_fetch_array($result3, SQLSRV_FETCH_ASSOC);
$row4 = sqlsrv_fetch_array($result4, SQLSRV_FETCH_ASSOC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM