简体   繁体   English

使用 PHP 从 2 个表中获取记录返回多个结果

[英]Get record from 2 tables using PHP returning multiple results

I have 2 tables.我有2张桌子。 One called local_requestors and one called local_aid .一种称为local_requestors ,一种称为local_aid They both have different sets of data, but they both share a column, the column in local_requestors called suser_scheme is the same as the data in the local_aid table column called scheme_id .它们都有不同的数据集,但它们共享一个列, local_requestors名为suser_scheme的列与local_aid表中名为scheme_id列中的数据相同。

I want to query my tables to get all data from the local_requestors table, where the ID matches that of the row in the local_aid table.我想查询我的表以获取local_requestors表中的所有数据,其中IDlocal_aid表中的行的ID匹配。

I've used the following...我用过以下...

$stmt = $conn->prepare('SELECT * 
                        FROM local_requestors 
                            INNER JOIN local_aid 
                        WHERE local_requestors.user_scheme=local_aid.scheme_id 
                        AND local_requestors.id = :aid_id');

$stmt->execute(['aid_id' => $user_id]);
while($row = $stmt->fetch()) {
    echo '<p>' . $row['user_name'] . '</p>';
}

This is all on a local page that I can only access, but to query the table I post some $_GET[] variables...这一切都在我只能访问的本地页面上,但是为了查询表,我发布了一些$_GET[]变量...

$user_id = $_GET['id'];
$scheme_id = $_GET['scheme_id'];

No matter what I try, I keep getting the same row from the local_requestors table, repeated around 10 times.无论我尝试什么,我都会从local_requestors表中获取相同的行,重复大约 10 次。


My goal is to get print the name of the user in the record from local_aid and a number of related records from local_requstors .我的目标是从local_aid的记录中打印用户的姓名,并从local_requstors打印一些相关的记录。

echo 'Hello ' . $aid_name . ', there is ' . $rowCount . ' people that need help in your area.;

I think you may have ment我想你可能有心理

SELECT * 
FROM local_requestors 
    INNER JOIN local_aid ON local_requestors.user_scheme=local_aid.scheme_id 
WHERE local_requestors.id = :aid_id

And from your description of what you want you may want a LEFT JOIN instead of a INNER JOIN根据您对所需内容的描述,您可能需要LEFT JOIN而不是INNER JOIN

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

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