使用php显示来自两个sql表的结果

2hh7jdfx  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(465)

我有两张table,叫 post 以及
pcgames if $_GET['game']; is =action 它将从列类别搜索表柱和pcgames上的操作。
柱状图

id|category  |game
------------------
1 | action   |cabal
------------------
2 | strategy |blackdessert
------------------
3 | RPG      |MUlegend
------------------
4 | action   |ragnarok
------------------

pcgames表

id|category  |game
-------------------
1 | action   |solidsnake
-------------------
2 | action   |finalfantasy
-------------------
3 | RPG      |kingdomhearts
-----------------
4 | action   |tekken
-------------------

结果

no|category  |game
------------------
1 | action   |cabal
------------------
2 | action   |solidsnake
-------------------
3 | action   |finalfantasy
-------------------
4 | action   |tekken
-------------------

PHP

<?php
    $game = $_GET['game'];
    $ids = mysqli_real_escape_string($conDB,$id);
    $query = "SELECT * FROM `post` WHERE `game` ='" . $game . "'";
    $result = mysqli_query($conDB,$query);
    while($row = mysqli_fetch_array($result)) {
?> 
        <li> <a href="./post_list.html"><?php echo $row['title']; ?></a> </li>
<?php }; ?>
xpszyzbs

xpszyzbs1#

我想你的意思是从表post和pcgames中获取数据,并带有分类条件。如果你想这么做。在您的php上尝试以下操作:

<?php
    $game = $_GET['game'];
    $ids = mysqli_real_escape_string($conDB,$id);
    $no = 1;

    //this will be get data from post table
    $query = "SELECT * FROM `post` WHERE `category` ='" . $game . "'";
    $result = mysqli_query($conDB,$query); 

    //this will be get data from pcgames table
    $query1 = "SELECT * FROM `pcgames` WHERE `category` ='" . $game . "'";
    $result1 = mysqli_query($conDB,$query1);       
?>
<table border=1>
    <tr>
        <th>No</th> <th>Category</th> <th>Game</th>
    </tr>
    <?php  
    //fetch data from post table
    while($user_data = mysqli_fetch_array($result)) {         
        echo "<td>".$no++."</td>";
        echo "<td>".$user_data['category']."</td>";
        echo "<td>".$user_data['game']."</td>";     
    }
    //fetch data from pcgames table
    while($user_data1 = mysqli_fetch_array($result1)) {         
        echo "<td>".$no++."</td>";
        echo "<td>".$user_data1['category']."</td>";
        echo "<td>".$user_data1['game']."</td>";     
    }
    ?>
</table>

相关问题