Combine all select commands in a relational table in MySQL into one JSON column

I have a table with headers that include the id and name of individuals. Additionally, there are multiple tables containing specific details about each individual, such as phone number, address, gender, birthplace, etc.

How can I construct a SELECT command that joins all these tables, but returns the data in one JSON populated column?

The desired output format is: {"id":''236732", "name":"John Doe", "phone": "4875696", ... }

Is it possible to achieve this?

table_header

id name

1 John Doe

2 Jack Sparrow

3 Jane Doe

....

...

....

5000 Jane Silk

table_detail id phone_number 1 34574358 2 68538039 3 7537689237 .... ... ... 5000 89457987

table_detail2 id address 1 xxxxxxxx 2 xxxxxxxx 3 xxxxxxxx .... .... .... 5000 xxxxxxx

table_detail3 .... .... ..... .... .... table_detail10 (with nearly identical structures, containing over 5000 records each)

I aim to add an additional field of JSON type to the table_header, which consolidates all detailed information for each user.

Answer №1

Thinking about the concept of "as a single line" brings to mind an issue I encountered previously where the echo contained multiple JSON objects but was sent as one string. In order to handle this in client-side JS, I had to parse it like so:

var lines = JSON.parse('[' + this.responseText.replace(/}{/g, '},{') + ']');

The lack of separators between objects caused them to be treated as an array, leading to potential complications. Is this why you prefer a single JSON object? If that's the case, there are workarounds available. As Nick mentioned, the PHP implementation is straightforward:

echo json_encode($data);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upgrading outdated pagination mysql_query PHP code to PDO

Currently in the process of transitioning to PDO for database management, I have implemented a news section on my website. The database connection is located within the header of the site. While using the following code for pagination, I am struggling to ...

Storing data locally using HTML5 and connecting to databases

As I work on designing a website using html5, css, and JavaScript primarily for client-side validation, I am also developing an app that will retrieve data entered on the website from a database. For example, if a user searches for a contact, the informa ...

Issue with Laravel's with() method and search functionality using LIKE is not functioning as expected

My current setup involves 2 tables. One table is for storing debt information (id, amount, category_id), while the other table is used for debt categories (id, name). I am attempting to retrieve data based on each month from the debt table. However, I have ...

Verify if the parsed JSON data is equal to NSNULL

I need assistance with parsing a JSON response and checking if one of my keys is null. Here is the code I am currently using: var routingNumber = (dic.value(forKey: "result") as! NSDictionary).value(forKey: "routingNumber") as! String When I run this cod ...

The node server is experiencing difficulties connecting to the mysql database, resulting in a timed out connection error at Connection._handleConnectTimeout

Having trouble establishing a connection with the mysql database. Every time I attempt to start the node server, it keeps throwing a database connection error. The specific error message is as follows: connect ETIMEDOUT at Connection._handleConnectTimeou ...

Figure reference parsing using regular expressions

I'm currently working on developing a regular expression to extract references to figures within a text. The goal is to match the following scenarios: Fig* 1, 2, and 3 (not limited to just 3, any number) Fig* 1-3 Fig* 1 and 2 Fig* 1 Fig* 1 to 4 M ...

Obtaining an SQS message body that does not include any double quotation marks

Greetings! Currently, I am utilizing boto3 for sending and receiving SQS messages. When I send an SQS message, it looks like this: {"userid":1234,"ml_algorithm_type":1,"file_format":1,"file_path":"leu.gz"} However, upon message reception, I end up with th ...

Python and SQLAlchemy tutorial: Establishing a connection to a MySQL server with Cleartext Authentication enabled

Seeking assistance in accessing a MySQL server that only allows mysql_clear_password setup. I have successfully connected using the --enable-cleartext-plugin option through the command line: >> mysql --port NNN -u my_user -h DB.host.com --enable-cle ...

Looking to display blog entries instead of project listings

I have a theme that supports displaying Portfolio Items on a full-width portfolio Home page. However, these portfolio items do not support videos but I can embed videos in normal posts. What I need to do is change the homepage to display category posts in ...

Ways to retrieve json_decode() information following ajax transmission

Currently experimenting with an ajax script to enhance my skills in ajax. I am using the serializeArray() method to submit a form via ajax, however, although I can successfully pass the data, I am unable to perform any operations on it. var submit_post = ...

Converting an NSString to JSON format in iOS5: A Step-by-Step Guide

I have a series of String values in the following format: [INFO] [Tue Aug 21 14:54:22 2012] [ViewController] [26] [Hello] [;] My goal is to convert these strings into JSON using NSJSONSerialization. The code I am currently using for this conversion ...

Insert well-formed JSON into an HTML element

I'm facing a challenge while trying to dynamically embed a valid JSON array into HTML. The issue arises when the text contains special characters like quotes, apostrophes, or others that require escaping. Let me illustrate the problem with an example ...

What is the functionality behind a free hosting website?

Is anyone familiar with websites like Hostinghood, where users can create a subdomain and upload HTML, CSS, etc.? I'm curious about how they operate and how I can create a similar site. This is my first question here, so please respond instead of disl ...

Error: A SyntaxError was encountered due to a missing closing parenthesis after an argument list while writing JavaScript within PHP code

I'm facing an issue writing JavaScript within PHP code. Here's my script : echo ' <script>'; echo ' $(function(){'; echo ' x = parseInt($("#counter2").val());'; echo ' $("#add_row2").click(function(){&apo ...

The fluid table within the Bootstrap container is not adapting to different screen

Can't figure out why the bootstrap container fluid table is not responsive. Even though I used an example of My First Bootstrap Page that works fine and is responsive, this particular table remains unresponsive. I want to avoid using a scroll but will ...

use ajax to post saved data to a WebAPI in php

I have successfully implemented the code to save data in a custom table using Ajax. Now, I need to figure out how to send this data to an asp.Net API using js/jQuery. How can I achieve this? Below is my HTML form and JS code: <div id="inline1" class= ...

How can I retrieve the 15th minute of the following hour from a specified time using PHP?

Can you find the time of the next hour's 15th minute from a given time? For example: If the input is 2021-08-26 12:00:37, the output should be 2021-08-26 12:15:00 If the input is 2021-08-26 12:30:37, the output should be 2021-08-26 13:15:00 ...

Retrieve the character count of the text within a dynamically created div using AJAX

I have a .php file containing an array of strings. One of these strings is fetched by my page and inserted into an empty div named post. However, when I try to determine the length of the string in the post, I always receive the error: Uncaught TypeErro ...

Transferring data from a PhoneGap application to an XLS file and attaching it to an email for easy sharing

Is there any example available that demonstrates how to achieve the following functionality in Phonegap? My app retrieves data through AJAX from our server. I would like users to be able to export these results to an Excel spreadsheet which can then be at ...

Filtering Quantity Based on Specific Attribute in ReactJs

I want to implement a quantity filter that can be based on color, size, or both. For instance, when I select the red color, it should display the total quantity of red items available. However, if I choose both the color red and size small, it should show ...