使用 php 显示来自两个表 sql 的结果
Display Result from two tables sql using php
我有 2 个 table,分别是 post
和 pcgames
if $_GET['game']; is =action
它将在 table post 和 pcgames 列类别中搜索操作。
post table
id|category |game
------------------
1 | action |cabal
------------------
2 | strategy |blackdessert
------------------
3 | RPG |MUlegend
------------------
4 | action |ragnarok
------------------
pcgames table
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 }; ?>
我想你的意思是从 table 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>
我有 2 个 table,分别是 post
和 pcgames
if $_GET['game']; is =action
它将在 table post 和 pcgames 列类别中搜索操作。
post table
id|category |game
------------------
1 | action |cabal
------------------
2 | strategy |blackdessert
------------------
3 | RPG |MUlegend
------------------
4 | action |ragnarok
------------------
pcgames table
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 }; ?>
我想你的意思是从 table 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>