0

In below code, I am trying to pass values dynamically for "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";

Every time when I call URL in a browser, its display below error message:

"ReturnMessage":"AWB no with same order id ($row[order_id]) already exists","AWBNo":"$row[alternateno]"

But if I give static values for OrderNo (16445) & AWB (16445), then it works fine:

"ReturnMessage":"successful","AWBNo":"16445"

So it seems I am not passing values properly, please guide me on this.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Try change `array ( 'OrderNo' => '$row[order_id]', 'ManifestDetails' => array ( 'AWB' => '$row[alternateno]', 'PaymentStatus' => 'COD', ), );` to `array ( 'OrderNo' => $row['order_id'], 'ManifestDetails' => array ( 'AWB' => $row['alternateno'], 'PaymentStatus' => 'COD', ), );` – Masivuye Cokile Nov 16 '18 at 12:20
  • Your sql query does not have a where clause to load dynamic – Masivuye Cokile Nov 16 '18 at 12:33

3 Answers3

0

Remove the quotes of order_id and alternateno and try it

<?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);

 ?>
Siva
  • 1,481
  • 1
  • 18
  • 29
  • Thanks, i got `"ReturnMessage":"AWB no with same order id (false) already exists","AWBNo":"false","` –  Nov 16 '18 at 12:30
  • Means once i call the url with some order id & awb , again i should not call same `order id & awb`, it should be dynamically fetch from database..... how to do that ? –  Nov 16 '18 at 12:32
  • can you please check complete code [here](https://pastebin.com/sdzNLded) –  Nov 16 '18 at 13:26
  • @mickmackusa sorry, i will do changes according to your answer...... previously i got `undefined index` , so i used `isset()`.... –  Nov 16 '18 at 13:29
  • That's because there are no associative keys in `$row`. @vickeycolors – mickmackusa Nov 16 '18 at 13:29
0

You need to change :

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

To

array (
  'OrderNo' => $row['order_id'], 
  'ManifestDetails' => 
  array (
    'AWB' => $row['alternateno'],    
    'PaymentStatus' => 'COD',   
  ),
);
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
0

mysqli_fetch_row() generates an array of indexed arrays. Access the result set data using the column indexes [0] for order_id and [1] for alternateno. You must also remove the single quotes when storing $row[0] and $row[1] in $data.

Right now, your query will be returning a result set of n rows. your while() loop will be overwriting and overwriting and overwriting $data and only preserving the final iteration's row data.

If you want to store all of the rows' data to $data, then write $data[] to push new row-data into your $data array.

Untested Code:

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);
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @vickey I have repaired your broken code. If there are more problems, they are to do with your data, not the code. Please improve your question. There is nothing more for me to solve at this point. – mickmackusa Nov 16 '18 at 13:19
  • Thanks, here is [complete code link](https://pastebin.com/15iga6yC) , here is [result](http://sbdev1.kidsdial.com/ecom1/example2.php) –  Nov 16 '18 at 13:21
  • I don't know what you are showing me. I can tell you that this is nonsense: `'OrderNo' => isset($row[1]),` <-- `isset()` will return either `true` or `false`. You need to remove `isset()` and just write `$row[1]` – mickmackusa Nov 16 '18 at 13:23
  • first time if i call [url ](http://sbdev1.kidsdial.com/ecom1/example2.php) , i want to pass first rows of `order_id` & `awb` , second time if i call, i want to pass `second rows of `order_id` & `awb`` .... so on.... so that everytime i will get success message if i use different `order_id` & `awb`.... –  Nov 16 '18 at 13:34
  • after i tried this code : https://pastebin.com/Uvd76S1y , now i got ""ReturnMessage":"AWB number not provided","AWBNo":null," along with Notice: Undefined offset.... but i passed AWB number with `'AirWayBillNO' => $row[66],` in [url](http://sbdev1.kidsdial.com/ecom1/example2.php) –  Nov 16 '18 at 13:43
  • sorry `$row[66]` , that is i am saving awb numbers there : https://prnt.sc/lj5suc , am i wrong ? –  Nov 16 '18 at 13:48
  • The number that is written into `$row[]` is the index of the column number (as I already explained in my answer). `[0]` for `order_id` and `[1]` for `alternateno`. There is no `[2]` there is no `[10]` there is no `[66]` because your `SELECT` clause only has two columns being delivered to the result set. The number is the column IN the currently iterated row. – mickmackusa Nov 16 '18 at 13:51
  • i tried this code : https://pastebin.com/eekjWM0N, i got result as [link](http://sbdev1.kidsdial.com/ecom1/example2.php) its loading too slowly, may be we wrote curl code inside while loop, in link, you can see we got `success` message in `1st row, after that its display `AWB no with same order id (9535356337) already exists`...... –  Nov 16 '18 at 13:57
  • It should be `'OrderNo' => $row[0],` because the orderno is the first column in your SELECT. `'AirWayBillNO' => $row[1],` is correct. – mickmackusa Nov 16 '18 at 13:59
  • Thanks , now [here](http://sbdev1.kidsdial.com/ecom1/example5.php) is result.... its fetching all the column values.... but at a time, i want to fetch only one `OrderNo & one AirWayBillNo`.... something like [link2](http://sbdev1.kidsdial.com/ecom1/example.php) –  Nov 19 '18 at 05:24
  • My code IS fetching one at a time. The output is merely separated by `\n` as indicated by your original post. HTML doesn't render `\n` as a line break though, it is just a space. My code is working as it should. If you only want one row total, you will need to limit your query and probably use a WHERE clause to isolate your desired database table row. – mickmackusa Nov 19 '18 at 06:23