我想捕获 $status 并将其插入数据库“smsdb”中。但是,我无法这样做,希望有人能指导我。 此起始代码是 wall 函数的一部分,这就是调用该函数后获取状态的地方。在grabdetails函数中,我将其他详细信息获取到数据库中,$status无法访问。请有人带我... //代码
$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);
有人吗?
最佳答案
$status
变量超出范围
。
添加行:
全局$status;
到你的函数。
http://php.net/manual/en/language.variables.scope.php
编辑:
当您在 wall()
函数中调用 grabdetails
时,$status
变量尚未设置。也许可以将状态作为参数传递到 wall()
函数中的 grabdetails
函数中。
例如grabdetails($message_sent, $mobilenumber, $result);
还将函数声明更改为grabdetails($messagee, $mobno, $status);
关于PHP Mysql 从函数将数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37788288/