脚本写入数据库(mysql)的三个变量

时间:2014-09-14 08:54:00

标签: php

我怎样才能在mysql数据库中写下这些变量--- 消息,serviceid& subscriberNo使用php?

代码如下所示

     $myfile = fopen("message","serviceId","subscriberNo", "w") or die("Unable to open file!");
     $message = "This is sample message";
     $serviceId="6013";
     $subscriberNo="254768254545|254956219273|254771514662|25495621145|254771514209";
     fwrite($myfile, $mesaage,$serviceId,$subscriberNo);
     fclose($myfile);   

对bulksms的HTTP请求           //初始化请求变量

         $request = "";

    $param['username'] = ""; 
    $param['password'] = ""; 
    $param['message'] = "This is sample message.";
    $param['subscriberNo'] = "254768254545|254956219273|254771514662|25495621145|254771514209"; 
    $param['serviceId'] = "6013";



    //traverse through each member of the param array 

    foreach($param as $key=>$val) 


            { 
    //we have to urlencode the values

        $request.= $key."=".urlencode($val); 
    //append the ampersand (&) sign after each parameter/value pair

            $request.= "&";



                }
         remove the final ampersand (&) sign from the request 

            $request = substr($request, 0, strlen($request)-1); 

                echo $request;

2 个答案:

答案 0 :(得分:0)

只需将它们存储在像这样的数组中

$param['username'] = ""; 
$param['password'] = ""; 
$param['message'] = "This is sample message.";
$param['subscriberNo'] = "254768254545|254956219273|254771514662|25495621145|254771514209"; 
$param['serviceId'] = "6013";

现在使用serialize($param)对其进行序列化 将其存储在mysql数据库中

如果要使用它,请在从mysql获取数据后使用deserialize($param)

答案 1 :(得分:0)

您不需要手动编码,只需使用http_build_query()

示例:

$param['username'] = ""; 
$param['password'] = ""; 
$param['message'] = "This is sample message.";
$param['subscriberNo'] = "254768254545|254956219273|254771514662|25495621145|254771514209"; 
$param['serviceId'] = "6013";

$query_string = http_build_query($param);
echo $query_string;

// username=&password=&message=This+is+sample+message.&subscriberNo=254768254545%7C254956219273%7C254771514662%7C25495621145%7C254771514209&serviceId=6013

unset($param['username']);
unset($param['password']);
$db = new mysqli('localhost', 'username', 'password', 'database_name');
$columns = implode(', ', array_keys($param));
$values = implode("', '", $param);
$sql = "INSERT INTO `messages` ($columns) VALUES ('$values')";
$query = $db->query($sql);