gpt4 book ai didi

php - mysqli 根据行ID值显示数据

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:07 25 4
gpt4 key购买 nike

我需要根据其他表中的值在一行中显示一个文本,我将解释......一步一步来,一切都非常清楚。

名为“links”的表有一行名为“title_id”

来自“links”的这一行“title_id”在名为“id”的行中的其他名为“titles”的表中具有相同的值。

“标题”表有另一行称为“标题”- 这就是我需要以这种方式显示的内容...

a href= ..../>TABLE links row title_id = to TABLE titles row id -> DISPLAY HERE table titles row title (based on row title_id from the table links)

希望你能看懂,结果一定是(a href=...>TEXT MOVIE TITLE)

这是我需要更改的基本代码(我已经评论了需要更改的部分):

    <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, label, title_id, season, episode, approved FROM links WHERE approved = 1 order by id desc LIMIT 30 OFFSET 1";
$result = $last_id = $conn->query($sql);


if ($result->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Audio</th><th>URL</th><th>Temporada</th><th>Episodio</th><th>Aprobado</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["label"]."</td>";
echo "<td>";
if (empty($row['episode'])) {
echo "<a href=";
echo '/peliculas-online/'.$row["title_id"];
echo ">HERE MUST HAVE THE VALUE FROM TABLE titles -> row title - based on $row["title_id"] </a>";
// -------------------------
}
else {
echo "<a href=";
echo '/series-online/'.$row['title_id'].'/seasons/'.$row['season'].'/episodes/'.$row["episode"];
echo ">HERE MUST HAVE THE VALUE FROM TABLE titles -> row title - based on $row["title_id"] </a>";
// -----------------------------------
}
echo "</td>";
echo "<td>".$row["season"]."</td><td>".$row["episode"]."</td><td>".$row["approved"]."</td></tr>";


}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

最佳答案

您只需稍微修改查询即可获取此信息。您可以像这样在这些 ID 上加入标题:

SELECT l.id, l.label, l.title_id, t.title, l.season, l.episode, l.approved 
FROM links l
JOIN titles t
ON l.title_id = t.id
WHERE approved = 1
ORDER by id DESC
LIMIT 30 OFFSET 1

然后您可以像以前一样调用变量;相同的查询将有 row['title']

了解更多关于 MySQL Joins 的信息

要在您的脚本中获取此内容,只需替换此行:

echo ">HERE MUST HAVE THE VALUE FROM TABLE titles -> row title - based on $row["title_id"] </a>";

用这个:

echo '>'.$row['title'].'</a>';

关于php - mysqli 根据行ID值显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124786/

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