2011-11-12 115 views
0

通數據庫郵件參數存儲prcedure我想用數據庫郵件我的應用程序,這意味着我需要將參數傳遞到存儲過程sp_send_dbmail 只是出於測試目的,我有以下的代碼。然而,我想知道如何使用ssql server 2008和php將參數傳遞給存儲過程。 僅供參考我使用SQLSRV驅動程序從微軟使用PHP和SQL Server 2008

<?php require_once ('../Connection/connmail.php')?> 
<?php 
$sql = "{CALL sp_send_dbmail[[@profile_name='gmailsmtp']]}";//my stored procedure 

    $stmt = sqlsrv_query($conn,$sql)or die(print_r(sqlsrv_errors(),true));  


?> 

上面的代碼產生錯誤

Array ([0] => Array ([0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '{'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '{'.) [1] => Array ([0] => 42000 [SQLSTATE] => 42000 [1] => 105 [code] => 105 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Unclosed quotation mark after the character string '[@profile_name='gmailsmtp']}'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Unclosed quotation mark after the character string '[@profile_name='gmailsmtp']}'.)) 

任何幫助將不勝感激

回答

0

我看到了,這是沒有答案了一段時間,所以這裏是解。讓您的用戶在msdb系統數據庫中的databasemailuserrole

$callmailproc = "EXEC msdb.dbo.sp_send_dbmail @profile_name = ?, @recipients=?, @subject=?, @body=?"; 
$profilename = 'DatabaseMail'; 
$recipients = '[email protected]'; 
$subject='Test Message'; 
$body = 'This is the body'; 
$params = array( 
       array($profilename, SQLSRV_PARAM_IN), 
       array($recipients, SQLSRV_PARAM_IN), 
       array($subject, SQLSRV_PARAM_IN), 
       array($body, SQLSRV_PARAM_IN), 
       ); 

/* Execute the query. */ 
$stmt3 = sqlsrv_query($conn, $callmailproc, $params); 
if($stmt3 === false) 
{ 
    echo "Error in executing statement 3.\n"; 
    die(print_r(sqlsrv_errors(), true)); 
}