Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Join two separate mysql queries into the same json data object
    primarykey
    data
    text
    <p>I'm trying to mesh the below mysql query results into a single json object, but not quite sure how to do it properly.</p> <pre><code>$id = $_POST['id']; $sql = "SELECT contracts.po_number, contracts.start_date, contracts.end_date, contracts.description, contracts.taa_required, contracts.account_overdue, jobs.id AS jobs_id, jobs.job_number, companies.id AS companies_id, companies.name AS companies_name FROM contracts LEFT JOIN jobs ON contracts.job_id = jobs.id LEFT JOIN companies ON contracts.company_id = companies.id WHERE contracts.id = '$id' ORDER BY contracts.end_date"; $sql2 = "SELECT types_id FROM contracts_types WHERE contracts_id = '$id'"; //return data $sql_result = mysql_query($sql,$connection) or die ("Fail."); $arr = array(); while($obj = mysql_fetch_object($sql_result)) { $arr[] = $obj; } echo json_encode($arr); //return json //plus the selected options $sql_result2 = mysql_query($sql2,$connection) or die ("Fail."); $arr2 = array(); while($obj2 = mysql_fetch_object($sql_result2)) { $arr2[] = $obj2; } echo json_encode($arr2); //return json </code></pre> <p>Here's the current result:</p> <pre><code>[{"po_number":"test","start_date":"1261116000","end_date":"1262239200","description":"test","taa_required":"0","account_overdue":"1","jobs_id":null,"job_number":null,"companies_id":"4","companies_name":"Primacore Inc."}][{"types_id":"37"},{"types_id":"4"}] </code></pre> <p>Notice how the last section [{"types_id":"37"},{"types_id":"4"}] is placed into a separate chunk under root. I'm wanting it to be nested inside the first branch under a name like, "types".</p> <p>I think my question has more to do with Php array manipulation, but I'm not the best with that.</p> <p>Thank you for any guidance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload