1

I need to fetch multiple data in an array using MySQL and PHP but I'm unable to do that. I am explaining my query below.

$sql = "
    SELECT s.id, s.voucher_code, s.merchant, s.date, s.receiver, s.sender, s.serial_no, s.image, s.expired_date, c.status,sup.supplier_id, sup.name, a.name AS sender_name, v.discount
    FROM db_send_evoucher_code s
    INNER JOIN db_code c ON s.voucher_code=c.total_voucher_code
    INNER JOIN db_supplier sup ON s.merchant=sup.supplier_id
    INNER JOIN medilink_admin a ON s.sender=a.admin_id
    INNER JOIN db_voucher_code v ON c.voucher_code_id=v.voucher_code_id
    WHERE s.receiver='".$userid ."' and c.status='sent'
";
$query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($query)) {
                $data = array(
                    "data" => array(
                        "voucher_code" => $row['voucher_code'], 
                        "send_by" => $row['sender_name'],
                        "image" => $row['image'],
                        "expired_date" => $row['expired_date'],
                        "supplier_name" => $row['name'],
                        "sending_date" => $row['date'],
                        "supplier_id" => $row['supplier_id'],
                        "discount" => $row['discount'],
                        "imagepath" => $imagepath
                    )
                );
            }
echo json_encode($data, JSON_UNESCAPED_SLASHES);    

Here I am getting only one set of values, but I need to fetch multiple values.

1
  • Please do at least elementary formatting of your code next time, so it's actually readable for others. Commented Feb 17, 2016 at 11:07

1 Answer 1

2

You are re affecting $data every time in your while.

<?php
$sql="select s.id,s.voucher_code,s.merchant,s.date,s.receiver,s.sender,s.serial_no,s.image,s.expired_date,c.status,sup. supplier_id,sup.name,a.name AS sender_name,v.discount from db_send_evoucher_code s INNER JOIN db_code c ON s.voucher_code=c.total_voucher_code INNER JOIN db_supplier sup ON s.merchant=sup.supplier_id INNER JOIN medilink_admin a ON s.sender=a.admin_id INNER JOIN db_voucher_code v ON c.voucher_code_id=v.voucher_code_id where s.receiver='".$userid ."' and c.status='sent' ";
$query=mysqli_query($con,$sql);
$data = array();
$data['data'] = array();
while($row=mysqli_fetch_array($query)){
    $data['data'][]=array("voucher_code"=>$row['voucher_code'],"send_by"=>$row['sender_name'],"image"=>$row['image'],"expired_date"=>$row['expired_date'],"supplier_name"=>$row['name'],"sending_date"=>$row['date'],"supplier_id"=>$row['supplier_id'],"discount"=>$row['discount'],"imagepath"=>$imagepath);
}
echo json_encode($data,JSON_UNESCAPED_SLASHES);  
Sign up to request clarification or add additional context in comments.

1 Comment

@ Gwendal : but here the data is repeating each iteration loop.here i need the output like this {"data":[{1st set data},{2nd set data},...]}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.