gpt4 book ai didi

php - 如何选择数据库的所有表mySql并按数据显示查询顺序,表具有相同的结构

转载 作者:行者123 更新时间:2023-11-29 21:03:40 25 4
gpt4 key购买 nike

我想制作一个新闻栏,显示我需要显示最后 5 个查询,我在同一个数据库 mySql 中有 5 个类别(表),例如历史、自然、生活方式、科学和旅行所有表都具有相同的结构id、标题、描述、数据、图片、alt、已批准,我想按数据选择最后 5 个订单,我该怎么做?以及显示查询的 php 代码

我的用于按同一表中的数据显示最后 5 个查询顺序的代码是这样的...

$sql = "SELECT id, titlu, link, poza, alt FROM istorie WHERE approved='1' ORDER BY RAND() LIMIT 3";
$query = mysqli_query($db_conx, $sql);
$istorielist = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$titlu = $row["titlu"];
$link = $row["link"];
$poza = $row["poza"];
$alt = $row["alt"];
$istorielist .= '<div id="articol-content-more"><a href="/istorie/'.$link.'"><img src="/images/'.$poza.'.jpg"class="articol-content-more-image" alt="'.$alt.'"><p class="articol-content-more-title">'.$titlu.'</p></a><span><a class="articol-content-more-afla" href="/istorie/'.$link.'">Citește mai multe</a></span><span class="articol-content-more-fl"><div class="fb-share-button" data-layout="button_count" data-href="http://esticurios.ro/istorie/'.$link.'"></div></span></div>';
}
?>
<p class="moreArticle">Mai multe articole:<br>
<?php echo $istorielist;?> <br>
<?php mysqli_close($db_conx);?>

最佳答案

您可以将类别数组静态化,或者将所有类别放入一个表中并动态创建类别数组

<?
$catArray = array("history","nature", "lifestyle", "science","travel");
$istorielist = '';
foreach($catArray as $k=>$v)
{
$sql = "SELECT id, titlu, link, poza, alt FROM ".$v." WHERE approved='1' ORDER BY RAND() LIMIT 3";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$titlu = $row["titlu"];
$link = $row["link"];
$poza = $row["poza"];
$alt = $row["alt"];
$istorielist .= '<div id="articol-content-more"><a href="/istorie/'.$link.'"><img src="/images/'.$poza.'.jpg"class="articol-content-more-image" alt="'.$alt.'"><p class="articol-content-more-title">'.$titlu.'</p></a><span><a class="articol-content-more-afla" href="/istorie/'.$link.'">Citește mai multe</a></span><span class="articol-content-more-fl"><div class="fb-share-button" data-layout="button_count" data-href="http://esticurios.ro/istorie/'.$link.'"></div></span></div>';
}
}

?>


关于php - 如何选择数据库的所有表mySql并按数据显示查询顺序,表具有相同的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37002657/

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