gpt4 book ai didi

php - 如何根据另一个字符串的值获取表值?

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:22 24 4
gpt4 key购买 nike

我不确定我是否使用了上面的正确行话,但我想做的是遵循...我如何让 $author 根据帖子的 $author_id 与用户 ID 匹配来输出用户名(来自 mySQL 用户表)?

<?php 
include "inc/mysql-connect.php";
$DynamicFeed = "";
$sql = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 20");
$postCount = mysql_num_rows($sql); // count the output amount
if ($postCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$title = $row["title"];
$post = $row["post"];
$author_id = $row["author_id"];
$author = $row["author_id"];
$type = $row["type"];
$date_posted = $row["date_posted"];
$DynamicFeed .= "<div class=\"item\"><img class=\"img-type\" align=\"left\" src=\"img/$type.jpg\"> <p><a href=\"droplet.php?id=$id\"><b>$title</b></a><br />by <a href=\"profile.php?id=$author_id\">$author</a> on $date_posted<br /><br /> $post</p> </div>";
}
} else {
$DynamicFeed = "It would appear that there ar'nt any Droplets here...";
}
?>

最佳答案

您可以按如下方式使用 JOIN 语句:

$sql = mysql_query("SELECT posts.*,users.name FROM posts LEFT JOIN users on posts.author_id=users.id ORDER BY posts.id DESC LIMIT 20");

关于php - 如何根据另一个字符串的值获取表值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com