Outputting query results on the right place
<p>I have set up the following query based on <a href="https://stackoverflow.com/questions/6574564/php-mysql-search-multiple-tables-using-a-keyword">this</a> question:</p>

<pre><code>"(SELECT users_id, first_name, last_name, 'msg' as type FROM users 
 WHERE users_id LIKE '%" . $searchQuery ."%' 
 OR first_name LIKE '%" . $searchQuery ."%' 
 OR last_name LIKE '%" . $searchQuery ."%') 
 UNION
 (SELECT title, vimeo_id, upload_date, 'topic' as type FROM video 
 WHERE title LIKE '%" . $searchQuery ."%' 
 OR vimeo_id LIKE '%" . $searchQuery ."%')"
</code></pre>

<p>After the query is executed it should spit out the results like this:</p>

<pre><code>foreach($row as $field) {
    $details[] = array(
        // Fields uit de users table
        'user_id'       =&gt;  $field['users_id'],
        'first_name'    =&gt;  $field['first_name'],
        'last_name'     =&gt;  $field['last_name'],
        //Fields uit de video table
        'vimeo_id'      =&gt;  $field['vimeo_id'],
        'video_title'   =&gt;  $field['title'],
        'upload_date'   =&gt;  $field['upload_date']
    );
}
</code></pre>

<p>It works but I still encounter a problem. When I run the script with a searchterm that should be within the 'user' table there is no problem. When I run the script with a searchterm that should be within the 'video' table I still get results but it moves the results to the first part of the loop. That means I get the following output:</p>

<pre><code>[{"user_id":"Test","first_name":"151125560","last_name":"2016-01-08 10:49:32","vimeo_id":null,"video_title":null,"upload_date":null}]
</code></pre>

<p>While the video id should be <code>151125560</code>, the video title should be <code>test</code> and the upload_date should be <code>2016-01-08</code>.</p>

<p>I tried to solve this by adding <code>as type</code> but I have no clue on how to proceed from here. Could somebody please help me by moving the results to the right field?</p>