How to do MySQL Join and use PHP to get values

I am trying to write a mysql query to fetch data in this format:

<table>
    <caption>table title and/or explanatory text</caption>
    <thead>
        <tr>
            <th>User ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Field Name 1</th>
            <th>Field Name 2</th>
            <th>Field Name 3</th>
            <th>Field Name 4</th>
            <th>Field Name 5</th>
        </tr>
    </thead>
    <tbody>
    <?php 

        while ($row = mysqli_fetch_array($query)) {

        echo "<tr>
                <td>" .$row{'user_id'}. "</td>
                <td>" .$row{'first_name'}. "</td>
                <td>" .$row{'last_name'}. "</td>
                <td>" .$row{'field_name_1'}. "</td>
                <td>" .$row{'field_name_2'}. "</td>
                <td>" .$row{'field_name_3'}. "</td>
                <td>" .$row{'field_name_4'}. "</td>
                <td>" .$row{'field_name_5'}. "</td>
            </tr>";
        }
    ?>
    </tbody>
</table>

      

The tables in the database are formatted as follows.

Table: user_data p>

user_id      |     first_name     |    last_name     |
------------------------------------------------------
1            |     Jeff           |    Smith         |
------------------------------------------------------
2            |     Bob            |    Smith         |
------------------------------------------------------
3            |     Steve          |    Smith         |
------------------------------------------------------
4            |     Mary           |    Smith         |
------------------------------------------------------
5            |     Anna           |    Smith         |
------------------------------------------------------

      

Table: custom_fields

custom_field_id   |    name         |
-------------------------------------
3                 |    field name   |
-------------------------------------
5                 |    field name   |
-------------------------------------
7                 |    field name   |
-------------------------------------
9                 |    field name   |
-------------------------------------
11                |    field name   |
-------------------------------------

      

Table: custom_field_data p>

user_id      |     custom_field_id    |    value     |
------------------------------------------------------
1            |     3                  |    XXXX      |
------------------------------------------------------
1            |     5                  |    BBBB      |
------------------------------------------------------
1            |     7                  |    CCCC      |
------------------------------------------------------
1            |     9                  |    ZZZZ      |
------------------------------------------------------
1            |     11                 |    YYYY      |
------------------------------------------------------
2            |     3                  |    XXXX      |
------------------------------------------------------
2            |     5                  |    BBBB      |
------------------------------------------------------
2            |     7                  |    CCCC      |
------------------------------------------------------
2            |     9                  |    ZZZZ      |
------------------------------------------------------
3            |     3                  |    XXXX      |
------------------------------------------------------
3            |     5                  |    BBBB      |
------------------------------------------------------
3            |     9                  |    ZZZZ      |
------------------------------------------------------
3            |     11                 |    YYYY      |
------------------------------------------------------

      

I am looking for the best solution to query the data and then print it to screen using PHP or AJAX. Is it possible? Would it be better to use json? And a sample request would be great.

FYI: Ultimately, all data to be retrieved must be filtered on the screen. Thanks for the help.

My desired output would be

user_id      |     first_name     |    last_name     |  custom_field_3  |   custom_field_5  |   custom_field_7  |   custom_field_9  |   custom_field_11     |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
1            |     Jeff           |    Smith         |  XXXX            |   BBBB            |   CCCC            |   ZZZZ            |   YYYY                |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
2            |     Bob            |    Smith         |  XXXX            |   BBBB            |   CCCC            |   ZZZZ            |   YYYY                |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
3            |     Steve          |    Smith         |  XXXX            |   BBBB            |   CCCC            |   ZZZZ            |   YYYY                |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
4            |     Mary           |    Smith         |  XXXX            |   BBBB            |   CCCC            |   ZZZZ            |   YYYY                |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
5            |     Anna           |    Smith         |  XXXX            |   BBBB            |   CCCC            |   ZZZZ            |   YYYY                |
-------------------------------------------------------------------------------------------------------------------------------------------------------------

      

+3


source to share


4 answers


You can use the function group_concat

, https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat , with my previous request to get one record per user ...

If you had a SQLfiddle I could test this, but like not, I have no data to test with ...

select ud.firstname, ud.lastname, group_concat(cf.name) 
from user_data as ud 
join custom_field_data as cfd 
on cfd.user_id = ud.user_id 
join custom_fields as cf 
on cf.custom_field_id = cfd.custom_field_id

      



I tested this on 3 tables I have with a similar setup, so I think it should work; names are subject to change.

Update:

select ud.firstname, ud.lastname, group_concat(cf.name) 
from user_data as ud 
join custom_field_data as cfd 
on cfd.user_id = ud.user_id 
join custom_fields as cf 
on cf.custom_field_id = cfd.custom_field_id
group by ud.user_id

      

+3


source


Let's start with a simple connection to your table custom_field_data

to user_data

, where the values and field names are hard-coded:

SELECT 
 u.*,
 f1.value as "<Field 1 Name>",
 ...
 f2.value as "<Field N Name>"
FROM 
 user_data u LEFT JOIN 
 custom_field_data f1 ON u.user_id = f1.user_id AND f1.custom_field_id = 1
 LEFT JOIN
 custom_field_data f ON u.user_id = fn.user_id AND fn.custom_field_id = <N>

      

Now, if you want to extract data without hardcoding, you will need to build your SQL dynamically based on the data in the table custom_fields

:

SELECT * FROM custom_fields;

      

If some custom fields are never used against users, you would like to limit the number of custom fields as follows:



SELECT * FROM custom_fields f
WHERE EXISTS 
       (SELECT * FROM custom_field_data 
        WHERE f.custom_field_id = custom_field_id)

      

Finally, to create the required SQL in PHP, you need the following lines:

// Begin SELECT clause (static):
$sql = "SELECT u.*,"

// Add all fields that you selected form the custom_fields:
foreach ($result as $row) { // Don't forget to handle commas
  $sql = $sql + <expression that gets codes and name>
}

// Start FROM clause (static):
$sql = $sql + " FROM user_data u "


// Join to custom_field_data:
foreach ($result as $row) {
  $sql = $sql + " LEFT JOIN custom_field_data <alias expression> ON ..
}

      

At the end, you should end up with a SQL string that attaches users to custom_field_data

for each available custom field.

+1


source


perhaps o pull the data with $.ajax()

and PHP

. Create a file data.php

, we are going to load this file with ajax

. In the file, data.php

write this code.

$query = mysqli_query("select * from custom_field_data 
         inner join 
         user_data on user_data.user_id = custom_field_data.user_id
         inner join 
         custom_fields on custom_field_data.custom_field_id = custom_fields.custom_field_id");
<table>
<caption>table title and/or explanatory text</caption>
<thead>
    <tr>
        <th>User ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Field Name 1</th>
        <th>Field Name 2</th>
        <th>Field Name 3</th>
        <th>Field Name 4</th>
        <th>Field Name 5</th>
    </tr>
</thead>
<tbody>
 <?php 

    while ($row = mysqli_fetch_array($query)) {

    echo "<tr>
            <td>" .$row{'user_id'}. "</td>
            <td>" .$row{'first_name'}. "</td>
            <td>" .$row{'last_name'}. "</td>
            <td>" .$row{'field_name_1'}. "</td>
            <td>" .$row{'field_name_2'}. "</td>
            <td>" .$row{'field_name_3'}. "</td>
            <td>" .$row{'field_name_4'}. "</td>
            <td>" .$row{'field_name_5'}. "</td>
        </tr>";
    }
?>
</tbody>

      

On the main page, use button

to load data.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script type="text/javascript">
$(function(){
    $('#load-data').click(function(){
        $.post('data.php', function(data){
            $('#results').html(data);
        });
    });
});
</script>

      

example in sqlfiddle

+1


source


The best way in my opinion is to use a connection because it will reduce the size of the data transferred between the DB and PHP

You can use JSON as it is lightweight.

You can keep things hidden in a hidden box and use them to further filter on the client side completely

update -

For Sql server, I would write a query like this

select 
  t1.user_name,
  t1.first_name,
  t1.last_name,
  (select name from custom_fields 
   where custom_field_id= t2.custom_field_id) 
from
 user_data t1 left join
 custom_field_data t2 on t1.user_id=t2.userid

      

Change this to match my-sql

-2


source







All Articles