简体   繁体   English

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

[英]How to SELECT all the tables mySql of a db and show query order by data, tables have the same structure

I want to make a newsbar show i need to show the last 5 query, I have 5 categories(tables) like history, nature, lifestyle, science and travel in the same database mySql all the tables have the same structure id, title, descriere, data, picture, alt, approved and I want to selet the last 5 order by data, How i can do this? 我想做一个新闻栏显示我需要显示最后5个查询,我在同一个数据库中有5个类别(表),如历史,自然,生活方式,科学和旅行mySql所有表都具有相同的结构ID,标题,描述,数据,图片,alt,已批准,我想按数据选择最后5个订单,我该怎么做? and the php code to show the query 和PHP代码来显示查询

my code for a show the last 5 query order by data in the same tables is this... 我的代码用于显示同一表中按数据显示的最后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);?>

You can take category array static Or put all categories into one table and make category array dynamically also 您可以将类别数组设为静态,也可以将所有类别放入一个表中,并动态创建类别数组

<?
$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>';
    } 
}

?> ?>


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

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