PHP Mysql从函数插入语句

时间:2016-06-12 11:54:15

标签: php mysql

此代码从数据库获取值并形成sms模板,然后将moble数字和消息传递给webservice以发送短信。它是功能墙的一部分()......

     $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 = $sperm->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 )";

问题是如何获取$ status ind将其插入到数据库中,因为它不在函数中?请。帮忙,有人吗?

1 个答案:

答案 0 :(得分:0)

上面的代码并不完整,但我假设你在$client = new SoapClient("http://59.38.606.10:8080/smsengine/smsws?WSDL");所在的函数实际上是wall函数。如果是,那么该函数返回的内容,即$result实际上具有您需要的状态。因此,使用此代码片段(假设$ sperm是一个拼写错误,实际上应该是$ perm,来自wall函数的响应),您将获得wall()的响应,这是一个对象并具有您需要的状态。

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

如果那是对的,那么在调用wall函数中的grabdetails之前,你实际上已经拥有状态,你可以将它发送到这样的函数:

grabdetails($message_sent, $mobilenumber, $result->return);

然后更改grabdetails的定义以接收状态并在数据库插入中使用它。