如何在while循环中使用curl传递数据库表值?

时间:2018-11-16 12:15:34

标签: php arrays json curl resultset

在下面的代码中,我试图动态传递“ OrderNo&AWB ”的值。

$sql="SELECT order_id , alternateno FROM do_order";

$con=mysqli_connect("localhost","root","","do_management");

if ($result=mysqli_query($con,$sql))
{
    while ($row=mysqli_fetch_row($result))
    {
        $data = 
            array (
                'OrderNo' => '$row[order_id]', 
                'ManifestDetails' => 
                    array (
                        'AWB' => '$row[alternateno]',    
                        'PaymentStatus' => 'COD',   
                    ),
                );
    }
    mysqli_free_result($result);
}

mysqli_close($con);

$url = "http://1234.1234.1234.1234";
$data = json_encode($data);

$curl = curl_init($url);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

每次我在浏览器中调用URL时,其显示在错误消息下方:

  

“ ReturnMessage”:“具有相同订单ID($ row [order_id])的AWB编号已存在”,“ AWBNo”:“ $ row [alternateno]”

但是,如果我为OrderNo(16445)和AWB(16445)给出了静态值,那么它可以正常工作:

  

“ ReturnMessage”:“成功”,“ AWBNo”:“ 16445”

所以看来我没有正确传递值,请对此进行指导。

3 个答案:

答案 0 :(得分:1)

删除order_id和alternateno的引号,然后尝试

<?php

$sql="SELECT order_id , alternateno FROM do_order";

$con=mysqli_connect("localhost","root","","do_management");

if ($result=mysqli_query($con,$sql))
{
 while ($row=mysqli_fetch_row($result))
 {
  $data = 
  array (
      'OrderNo' => $row['order_id'], 
      'ManifestDetails' => 
         array (
           'AWB' => $row['alternateno'],    
           'PaymentStatus' => 'COD',   
               ),
        );
   }
  mysqli_free_result($result);
 }

 mysqli_close($con);

 ?>

答案 1 :(得分:1)

您需要更改:

array (
  'OrderNo' => '$row[order_id]', 
  'ManifestDetails' => 
  array (
    'AWB' => '$row[alternateno]',    
    'PaymentStatus' => 'COD',   
  ),
);

收件人

array (
  'OrderNo' => $row['order_id'], 
  'ManifestDetails' => 
  array (
    'AWB' => $row['alternateno'],    
    'PaymentStatus' => 'COD',   
  ),
);

答案 2 :(得分:1)

mysqli_fetch_row()生成索引数组的数组。使用[0]的列索引order_id[1]的列索引alternateno访问结果集数据。将$row[0]$row[1]存储在$data中时,还必须删除单引号。

现在,您的查询将返回一个 n 行的结果集。您的while()循环将覆盖,覆盖和覆盖$data,并且仅保留最终迭代的行数据。

如果要将所有行的数据存储到$data,请写入$data[]以将新的行数据推入$data数组中。

未经测试的代码:

if (!$con = mysqli_connect("localhost", "root", "", "do_management")) {
    echo "connection error";
} elseif (!$result = mysqli_query($con, "SELECT order_id, alternateno FROM do_order")) {
    echo "query error";
} else {
    $url = "http://114.143.206.69:803/StandardForwardStagingService.svc/AddManifestDetails";
    while ($row = mysqli_fetch_row($result)) { // fetch_row does not generate associative keys
        $data = [
            'OrderNo' => $row[0], 
            'ManifestDetails' => ['AWB' => $row[1], 'PaymentStatus' => 'COD']
        ];

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_VERBOSE, true);

        $curl_response = curl_exec($curl);
        curl_close($curl);
        echo $curl_response ."\n";
    }
    mysqli_free_result($result);
}