简体   繁体   English

PHP - 使用mysql两个表的用户成员列表

[英]PHP - user membership list using mysql two tables

I want to make a membership list to show from two tables.我想制作一个会员列表以从两个表中显示。

I want to join the two tables on there id's我想加入两个表上有 id 的

database table - user数据库表 - 用户

数据库表 - 用户, ,

database table - user_data数据库表 - user_data

数据库表 - user_data

Currently I'm getting only the id=1 from user table with user data as well.目前我也只从带有用户数据的用户表中获取id=1

I know I want to loop it to see all the records from the user table but I don't how too.我知道我想循环它以查看用户表中的所有记录,但我也不知道如何。 I am new to coding.我是编码新手。

{

    $result = mysql_query("SELECT * FROM user WHERE user_active='Yes' AND user_id!=0 ") or die(mysql_error()); 
     $row = mysql_fetch_array($result,MYSQL_ASSOC);

     foreach ($row as $key => $value) {
        $output[$key] = $value;
     } 

    $user_id=$output['user_id'];

        // now we get the user information and add it to the $output array.
     $result = mysql_query("SELECT data_type,data_value FROM user_data WHERE data_user_id=$user_id ") or die(mysql_error()); 
     while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {

        $output['user_data'][$row['data_type']] = $row['data_value'];

     }

    print_r($output);
}

Only one request is needed in this context you have to rewrite your SQL call.在此上下文中只需要一个请求,您必须重写 SQL 调用。

SELECT u.user_id, u.user_active, ud.data_type, ud.data_value FROM user u 
JOIN user_data ud 
ON u.user_id = ud.user_id 
WHERE u.user_active='Yes' AND data_user_id!=0

I JOIN the 2 tables with ALIAS tables u and ud (user,user_data) with the same id in your context user_id and data_user_id :2代表与名表u和UD(用户,USER_DATA)在您的上下文USER_ID和data_user_id相同的ID:

// now we get the user information and add it to the $output array.
$result = mysql_query("SELECT u.user_id, u.user_active, ud.data_type, ud.data_value FROM user u 
                       JOIN user_data ud 
                       ON u.user_id = ud.user_id 
                       WHERE u.user_active='Yes' AND data_user_id!=0") or die(mysql_error()); 
     while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {

        $output['user_data'][$row['data_type']] = $row['data_value'];

     }

print_r($output);

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM