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

时间:2018-04-17 02:06:02

标签: php mysql display

我有两张表,名为postpcgames

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

1 个答案:

答案 0 :(得分:0)

我认为你的意思是从表格帖子和带有类别条件的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>