2014-07-02 58 views
0

所以我有一個PHP腳本,我正在從MSSQL到SQLSRV,我有一些困難。sqlsvr_fetch_array fetch_array

我有這個

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); 

基本上拉基於搜索數據,然後穿過每行引用它們的數據。在MySQL中這不會是一個問題 - 但在這裏似乎有困難?

基本上,如果我搜索「史密斯」,它會帶出所有史密斯用戶的名字......(例如三行),那麼我希望它分別得到他們的每個餘額。

任何人都可以幫忙嗎?

謝謝。

回答

0

這將是固定的代碼。 sqlsrv_fetch_array的第一個參數必須是一個資源,你在這裏給出一個字符串。在「解壓縮」之前,您必須首先執行查詢。

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); 
+0

非常感謝你,這工作。你真的幫助我,我真的很感激! – user2720975

+0

沒問題,很樂意幫忙! :) – Gerdinand

0

您錯過了對每個查詢的sqlsrv_query調用。

即你應該有這樣的事情爲每個子查詢(見例1 http://www.php.net/manual/en/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); 

既然你已經使用MySQL和SQLServer的,它可能是值得考慮使用PDO( http://uk3.php.net/pdo)。這將允許您更改配置,而不必在需要更改數據庫服務器時編輯代碼。它也使代碼一致。

+0

謝謝你的幫忙! – user2720975

+0

如果您認爲這是答案,我會很感激,如果你可以設置它。 –

相關問題