0

So I'm using json_encode to fetch data from MySQL database. Fetching the data works just fine but I'm having problems when looping my query in a foreach loop statement.

Here's my code:

<?php
session_start();
include '../../dbconnect.php';
foreach($_SESSION['sonum'] as $value)
{
    //echo $value;
$rs = mysql_query("select products.product_name as 'product_name', products.product_desc as 'product_desc', order_details.srp as 'srp', order_details.quantity as 'quantity', order_details.commrate as 'comm', order_details.ma as 'ma', order_details.od_no as 'od_no', `sales-order`.`so-no` as 'sono' from products inner join order_details on products.product_id = order_details.product_id inner join `sales-order` on order_details.so_number = `sales-order`.`so-number` where order_details.so_number = '$value' and `po_number` = ''");

$result = array();

while($row = mysql_fetch_object($rs)){
    array_push($result, $row);
}

echo json_encode($result);
}
?>

Here's it's result which doesn't work:

[{"product_name":"3C3CRBSG28PWR93","product_desc":"3Com 3CRBSG28PWR93 Baseline Plus Switch 2928 HP V1910-24G-PoE (170W)\r\n","srp":"56000.00","quantity":"2","comm":"0.010","ma":"0.00","od_no":"30","sono":"ASDie"},{"product_name":"BRODCP-1510","product_desc":"Brother DCP-1510 Printer","srp":"68000.00","quantity":"2","comm":"0.010","ma":"0.00","od_no":"31","sono":"ASDie"}][{"product_name":"Intel DC","product_desc":"Intel Pentium Quad Core ","srp":"0.00","quantity":"0","comm":"0.000","ma":"0.00","od_no":"34","sono":"ry46"},{"product_name":"HP Pavillion TS 14-n039TU","product_desc":"3rd generation intel Core 13-3217u","srp":"0.00","quantity":"0","comm":"0.000","ma":"0.00","od_no":"35","sono":"ry46"}][{"product_name":"Intel DC","product_desc":"Intel Pentium Quad Core ","srp":"0.00","quantity":"0","comm":"0.000","ma":"0.00","od_no":"42","sono":"asdaewe"},{"product_name":"HP Pavillion TS 14-n039TU","product_desc":"3rd generation intel Core 13-3217u","srp":"0.00","quantity":"0","comm":"0.000","ma":"0.00","od_no":"43","sono":"asdaewe"}]

I am not sure what I'm doing wrong so it'll be a great help to point it out for me.

1
  • I have another php file, which displays the data in a table, that calls the page with the code I posted above (get.php). What I mean by not working is, the data doesn't display on the other page. If I place the end bracket of the foreach before echoing the json_encode, it works fine but the only data that's being fetched is the result of the query of the last $value. Commented May 26, 2014 at 5:49

1 Answer 1

1

Take some of your statements out of the loop:

<?php
session_start();
include '../../dbconnect.php';

$result = array();

foreach($_SESSION['sonum'] as $value) {
    //echo $value;
    $rs = mysql_query("select products.product_name as 'product_name', products.product_desc as 'product_desc', order_details.srp as 'srp', order_details.quantity as 'quantity', order_details.commrate as 'comm', order_details.ma as 'ma', order_details.od_no as 'od_no', `sales-order`.`so-no` as 'sono' from products inner join order_details on products.product_id = order_details.product_id inner join `sales-order` on order_details.so_number = `sales-order`.`so-number` where order_details.so_number = '$value' and `po_number` = ''");

    while($row = mysql_fetch_object($rs)){
        $result[] = $row;
    }
}

echo json_encode($result);
?>

Here I've declared $result as an array before looping. The problem you were having was that $result was being redeclared in each iteration as an empty array.

Also, the json_encode function comes after the loop as you only need to encode it after all of the elements are pushed into the array.

Another performance improvement would be to use the square bracket notation instead of array_push when adding one element to an array, hence $result[] = $row.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks for the explanation! I'm still new to json_encode and was experimenting on it. Thanks a lot :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.