Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让MySQL加入并使用PHP获取值_Php_Mysql_Ajax_Json_Entity Attribute Value - Fatal编程技术网

如何让MySQL加入并使用PHP获取值

如何让MySQL加入并使用PHP获取值,php,mysql,ajax,json,entity-attribute-value,Php,Mysql,Ajax,Json,Entity Attribute Value,我正在尝试编写一个mysql查询,以提取以下格式的数据: <table> <caption>table title and/or explanatory text</caption> <thead> <tr> <th>User ID</th> <th>First Name</th> &l

我正在尝试编写一个mysql查询,以提取以下格式的数据:

<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>
表:自定义字段

custom_field_id   |    name         |
-------------------------------------
3                 |    field name   |
-------------------------------------
5                 |    field name   |
-------------------------------------
7                 |    field name   |
-------------------------------------
9                 |    field name   |
-------------------------------------
11                |    field name   |
-------------------------------------
表:自定义字段数据

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      |
------------------------------------------------------
我正在寻找查询数据的最佳解决方案,然后使用PHP或AJAX将其打印到屏幕上。这可能吗?使用json会更好吗? 一个示例查询就好了

仅供参考:从长远来看,所有提取的数据都需要在屏幕上进行过滤。谢谢你的帮助

我期望的输出是

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                |
-------------------------------------------------------------------------------------------------------------------------------------------------------------

在我看来,最好的方法是使用join,因为它将减少在DB和PHP之间传输的数据的大小

您可以使用JSON,因为它很轻

您可以在hidden字段中隐藏这些内容,并将其用于客户端的进一步过滤

更新--

对于Sql server,我会编写如下查询

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

根据我的sql将此更改让我们从
自定义_字段_数据
简单连接到
用户_数据
表,其中值和字段名称是硬编码的:

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>
如果某些自定义字段从未与用户相关使用,则需要限制自定义字段的数量,如下所示:

SELECT * FROM custom_fields f
WHERE EXISTS 
       (SELECT * FROM custom_field_data 
        WHERE f.custom_field_id = custom_field_id)
最后,要在PHP中构建所需的SQL,您需要以下字符串部分:

// 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 ..
}
//开始选择子句(静态):
$sql=“选择u.*”
//添加从自定义_字段中选择的所有字段:
foreach($result as$row){//不要忘记处理逗号
$sql=$sql+
}
//起始从句(静态):
$sql=$sql+“来自用户数据”
//加入自定义\u字段\u数据:
foreach($结果为$行){
$sql=$sql+“左连接自定义字段\数据打开..”。。
}

最后,您应该使用SQL获得一个字符串,该字符串将用户连接到每个可用自定义字段的
自定义字段\u数据。

可以使用
$.ajax()提取数据
PHP
。创建一个文件
data.PHP
我们将使用
ajax
加载此文件。在您的
data.PHP
文件中编写此代码

$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>

您可以使用
group\u concat
功能,与我之前的查询一起为每个用户获取一条记录

如果你有一个SQLfiddle,我可以测试出来,但我没有任何数据可以测试

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
我用类似的设置在3个表上进行了测试,所以我认为它应该可以工作;名称可能需要调整

更新:

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

谢谢你,Manju,我正在寻找一个类似这样的示例查询…有什么想法吗?这不会产生预期的结果。我已经编写了与sql server相关的查询,但没有在MySQL中编写。该查询是通过记住逻辑编写的…无论如何,我们可以将查询转换为更有效的…但它是按逻辑运行的。我认为您需要这样做扩展您的答案,因为现在我看不出您的查询结果如何与所讨论的任务相匹配。
user\u id
custom\u field
的关系如何?
value
name
来自
custom\u fields
?这是join doc.。应该能够确定数据的关系。这种关系类似于
user_data.user\u id->custom\u field\u data.user\u id->custom\u fields.name
…有意义吗?我一开始误解了这个问题,以为第三个表是您想要的视图。我想这可能是您想要的
从用户数据中选择*作为ud加入自定义字段\u数据作为cfd上的cfd。user\u id=ud.user\u id作为cf上的cf加入自定义字段。自定义字段\u id=cfd.custom_field_id
@chris85-我成功地编辑了您的查询,以获得数据部分所需的内容。现在的问题是,它正在为每个自定义字段打印一行……我如何循环并生成上面的HTML?嘿@chris85,我想我已经接近了。现在我只获得要输出的第一行。添加
分组方式ud.userid
。我只测试了一条记录,所以以前没有遇到过。Ok@chris85-我在这里得到了它的工作,-现在我需要知道如何在PHP中获取每个值以打印表数据。使用当前使用的相同的获取和回显。使用别名使列名与以前相同。因此,在输出的数组中,它看起来像是,…我不知道如何在组合分组字段时循环它们?例如“27Noe”,其中27是cf id,Noe是值…有什么帮助吗?
<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>
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
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