0

I need to append result of mysql query with existing result of another query, and both queries performed on different table, actually what I really want to do is take value from main table and get some data from another table using first value, and combine all together using json encode.

$query = "select ID,TIMESTAMP,UUID from  TABLE1 where USERNAME='$user'";
$result  = mysqli_query($conn, $query);

 $numrows = mysqli_num_rows($result);
 if($numrows>0)
 {
   $res=$a;
   $myArray = array();
   while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $eachuuid =  $row[UUID];
        $query1 = "select ID,IMAGEURL from  TABLE_IMAGES where IMG_UUID='$eachuuid'";
        $result1  = mysqli_query($conn, $query1);
        $numrows1 = mysqli_num_rows($result);

        if($numrows1>0){
           $res1; 
           $myArray1 = array();
           while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
           $myArray1[] = $row1;
          }
          $row['IMG_URL']=$myArray1;
         }

      $myArray[] = $row;
   }

   $res['Data']=$myArray;
   echo json_encode($res);

Result:

{"Response":"OK","Data":[{"ID":"62",
                          "TIMESTAMP":"26 January",
                          "UUID":"12345",
                          "IMG_URL":[{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}]}]}

And I need to get the output like,

{"Response":"OK","Data":[{"ID":"62",
                              "TIMESTAMP":"26 January",
                              "UUID":"12345",
                              "IMG_URL":{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}}]}

Means need to remove [] from IMG_URL section so that I can parse the data easily.

2
  • Where is the value for $user coming from? If it's being submitted by the user it needs to be validated and a prepared statement used. For the second query, consider using IN for the WHERE clause instead of hitting the database multiple times. Is there a common field that could be used for a join? Commented Jan 27, 2016 at 6:41
  • The variable $user passed as an argument to the php while it's calling form client, and I didn't understand Is there a common field that could be used for a join? Commented Jan 27, 2016 at 6:52

1 Answer 1

1

You can do this

$row['IMG_URL']=$myArray1[0];

Or

  $myArray1 = null;
  while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
       $myArray1 = $row1;
  }
  $row['IMG_URL']=$myArray1;

I hope this helps.

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

Comments

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.