Php 在for each循环中进行查询?尝试获取返回的每行中每个外部ID号的用户名

Php 在for each循环中进行查询?尝试获取返回的每行中每个外部ID号的用户名,php,mysql,Php,Mysql,我试图为每条记录显示喜欢其他用户帖子的人的用户名。到目前为止,我只能知道如何显示从points.LID获得的用户ID号。下面的代码就是这样做的。我尝试了一些方法,但我不知道如何在foreach循环中获取第二个sql查询,以便为每个喜欢的用户获取LID/ID的用户名 我需要从LidSQL某处/以某种方式获取用户显示名称,以便我可以回显喜欢LID文章的人的姓名。我需要使用LID连接到wp_users表中的wp_users.ID,然后抓取wp_users.display_name。但是我可以在下面的f

我试图为每条记录显示喜欢其他用户帖子的人的用户名。到目前为止,我只能知道如何显示从points.LID获得的用户ID号。下面的代码就是这样做的。我尝试了一些方法,但我不知道如何在foreach循环中获取第二个sql查询,以便为每个喜欢的用户获取LID/ID的用户名

我需要从LidSQL某处/以某种方式获取用户显示名称,以便我可以回显喜欢LID文章的人的姓名。我需要使用LID连接到wp_users表中的wp_users.ID,然后抓取wp_users.display_name。但是我可以在下面的foreach循环中插入这样的查询吗

我想我需要添加一些东西,比如从points.ID=$user\u ID的点中选择points.LID,然后从wp\u users.ID=points.LID的点中获取显示名称

以下是包含相关列的3个表: 1.点ID、LID、SID、, 2.故事ID,故事名称 3.wp\u用户ID,显示\u名称

//only need current user for ID of author
$user_ID = get_current_user_id()

$results = $dbh->prepare("select 
stories.ID,
stories.SID,
stories.story_name,
points.ID,
points.LID,
points.PID,
wp_users.ID,
wp_users.display_name
FROM stories
JOIN points ON stories.SID=points.SID
JOIN wp_users ON stories.ID=wp_users.ID
where (stories.ID = $user_ID) and (PID = 1)");
$results->execute();

$row = $results->fetchAll(PDO::FETCH_ASSOC);

if ($row) {
echo '<table>';
echo '<tr>';
echo '<td><b>Name</b></td>';
echo '<td><b>Author</b></td>';
echo '</tr>';
foreach ($row as $all) {

//Get user display name from LID sql here? So I can echo name of person
//who liked the post (the LID) below? I need use LID to connect to
//wp_users.ID in wp_users table and then grab wp_users.display_name

$stp = stripslashes($all['story_name']);
echo '<tr>';
echo "<td><a href=\"http://example.com/complete-text?
writing=$all[SID]\">$stp</a></td>";
echo "<td><a href=\"http://example.com/portfolio?ID=$all[LID]\">
$all['LID']</a></td>";
}
echo '</table>';
}
?>
//此代码返回故事的名称和喜欢它的人的ID号。但是我需要它返回喜欢它的人的用户名wp\u users.display\u name

//only need current user for ID of author
$user_ID = get_current_user_id()

$results = $dbh->prepare("select 
stories.ID,
stories.SID,
stories.story_name,
points.ID,
points.LID,
points.PID,
wp_users.ID,
wp_users.display_name
FROM stories
JOIN points ON stories.SID=points.SID
JOIN wp_users ON stories.ID=wp_users.ID
where (stories.ID = $user_ID) and (PID = 1)");
$results->execute();

$row = $results->fetchAll(PDO::FETCH_ASSOC);

if ($row) {
echo '<table>';
echo '<tr>';
echo '<td><b>Name</b></td>';
echo '<td><b>Author</b></td>';
echo '</tr>';
foreach ($row as $all) {

//Get user display name from LID sql here? So I can echo name of person
//who liked the post (the LID) below? I need use LID to connect to
//wp_users.ID in wp_users table and then grab wp_users.display_name

$stp = stripslashes($all['story_name']);
echo '<tr>';
echo "<td><a href=\"http://example.com/complete-text?
writing=$all[SID]\">$stp</a></td>";
echo "<td><a href=\"http://example.com/portfolio?ID=$all[LID]\">
$all['LID']</a></td>";
}
echo '</table>';
}
?>
选择 .ID, 希德先生, 故事,故事的名字, points.ID, 点.LID, points.PID, wp_users.ID, wp_users.display_name, wp_users2.display_name为“喜欢的用户”, 从…起 故事 连接点 ON stories.SID=points.SID 加入wp_用户 ON stories.ID=wp_users.ID 作为wp_用户2左加入wp_用户 ON points.LID=wp_users2.ID 其中stories.ID=$user\u ID PID=1;
您是否尝试使用$all['display_name']而不是$all['LID']?是的,但这会为作者stories.ID的ID提供显示名称,而不是喜欢该故事的人。然后,您将不得不为问题添加更多信息。你怎么知道谁喜欢这个故事?什么是表格结构?表格结构在原始问题/帖子中提到。“点”表有一个LID列。那就是喜欢这个故事的人。但是那个人的用户名在wp_users.display_namesorry中插入那个sql到代码段,我的答案因为格式不正确而被拒绝谢谢,但我没有工作。我认为方法是我需要另外两个sql语句,尽管我不知道如何集成它们:A:从points.ID=$user\ID的点中选择points.LID B:然后获取显示名称where wp\u users.ID=points.LID是否确实像原始查询一样将stories.ID=1更改为stories.ID=$user\ID?是的。这样就得到了当前用户。我们想要当前用户的故事id,但是喜欢帖子的用户的like id LID这很奇怪,它应该根据points.LID=wp_users2.id来判断谁喜欢那篇文章,不管points.LID中是否有任何值。请看我修改过的答案,因为我在join中错误地键入==了。