PHP用户定义的函数问题

时间:2016-06-12 10:06:54

标签: php mysqli

我无法弄清楚为什么我无法获取发送消息的值$ message_sent和移动号码$ mobilenumber并在使用我的代码所示的grabdetails函数发送消息后将它们存储在数据库中下面。有人请指导我

//代码

 // fetch values from a database
 $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
    );
      // send message through a web service
    $client = new SoapClient("http://52.34.63.16:8080/smsengine/smsws?WSDL");

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

   // call function to grab mobile number and message
      grabdetails($message_sent, $mobilenumber);
    return $result;


 }

 // the function
 function grabdetails($messagee, $mobno)
   {

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


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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// insert into db
$sql = "INSERT INTO smsdb (sms_text, receiver_number, time_sent)
   VALUES
    ('$message_sent', '$mobilenumber', CURDATE() )";

1 个答案:

答案 0 :(得分:0)

如果在函数外定义某些内容,则需要使用“global”标记才能在函数内部使用它们。它基本上意味着您在函数内部使用全局变量。

在这种情况下,您只需要添加

global $message_sent,$mobilenumber;