2016-06-13 97 views
0

我想要捕獲$狀態並將其插入到數據庫的'smsdb'中。但是,我無法這樣做,並希望有人會指導我。 此起始代碼是牆功能的一部分,並且在調用該函數之後獲取狀態。在grabdetails函數中,我將其他詳細信息導入到db中,$ status是無法訪問的。會有人領我請... //代碼PHP Mysql插入數據到數據庫中的函數

$name = $resultarr['name']; 
$amount = $resultarr['amount']; 
$transaction_id = $resultarr['trans_id']; 
$date = $resultarr['time_paid']; 

//message template 
$message = "Dear $name we have received $amount from you. MPESA transaction Id $transaction_id on $date."; 

$mobilenumber = $resultarr['msisdn']; // get mobile number from array 
$message_sent = $message; 

$serviceArguments = array(
     "mobilenumber" => $mobilenumber, 
     "message" => $message_sent 
); 

$client = new SoapClient("http://59.38.606.10:8080/smsengine/smsws?WSDL"); 

$result = $client->process($serviceArguments); 

grabdetails($message_sent, $mobilenumber); 

return $result; 

} 
//I call the function wall() to send sms   

wall(); 
$perm = wall(); 
$status = $perm->return; //outputing the status 
    // Here I want to capture the $status variable and put it in a db below 
echo "$status"; 


function grabdetails($messagee, $mobno) 
{ 

$message_sent = $messagee; 
$mobilenumber = $mobno; 


$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "smsdb"; 

// Create connection 


// Check connection 

$sql = "INSERT INTO smsdb (sms_text, receiver_number, time_sent, status) 
    VALUES 
    ('$message_sent', '$mobilenumber', NOW(), '$status')"; 
$conn->query($sql); 

任何一個?

+0

請給予適當的打開和關閉括號在代碼 –

+0

到底在哪?請致電 –

+0

此功能grabdetails在哪裏結束?它沒有右花括號,功能牆在哪裏? –

回答

1

$status變量是out of scope

添加一行:

global $status;

給你的函數。

http://php.net/manual/en/language.variables.scope.php

編輯:

當你wall()函數中調用grabdetails,在$status變量尚未設置。可能在wall()函數中將狀態作爲grabdetails函數中的參數傳遞。

例如grabdetails($message_sent, $mobilenumber, $result);

也改變了函數聲明grabdetails($messagee, $mobno, $status);

+0

我應該包括全球在這裏全球$狀態= $ perm->返回;?而當插入數據庫? –

+0

函數grabdetails($ messagee,$ mobno,$ status) {像這樣?在牆上的哪個函數需要兩個參數?我的意思是在這裏grabdetails($ message_sent,$ mobilenumber); –

+0

當您調用方法時,將'result'變量作爲參數添加。 'grabdetails($ message_sent,$ mobilenumber,$ result);'。不是'$ result-> return == $ status'? – user6459501