【问题标题】:How to get sql data from two tables and print to one?如何从两个表中获取sql数据并打印到一个?
【发布时间】:2017-05-28 10:41:56
【问题描述】:

我是 PHP 和 SQL 的新手。我还在学习。 因此,我的 DB 3 表中有名为 ps_plrps_plr_ids_nameps_plr_victms

  • ps_plr,我需要列:rank
  • ps_plr_ids_name,我需要列:name
  • ps_plr_victms,我需要 2 列:killsdeaths

在所有这些表中,我都有将作为基础的 plrid 列。

我需要输出一个 html 表格,其中包含: 排名、姓名、杀戮和死亡

我尝试了以下可以打印排名和plrid的代码,但是我真的不需要桌子上的plrid,就像我说的那样在其他桌子上找到我需要的东西的基地。

<?php
$servername = "localhost";
$username = "root";
$password = "********";
$dbname = "psychostats3_1";

$mysqli = new mysqli($servername,$username,$password,$dbname);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT ps_plr.rank, ps_plr.plrid FROM ps_plr;";
$query .= "SELECT ps_plr_ids_name.name FROM ps_plr_ids_name plrnk, ps_plr plrst;";
$query .= "WHERE plrid=name";


//select task.id, task.name, proj.id, proj.name
//from tasks task, projects proj
//where proj.id=task.project_id; 

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("<tr><td>" . $row[0] . "</td>");
                printf("<td>" . $row['2'] . "</td></tr>");
            }
            $result->free();
        }
        /* print divider */
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

【问题讨论】:

  • 你能显示两个表的表结构吗?
  • 加入pri_id上的三个表并使用where子句,因为plrid='name' id name is string。

标签: php html mysql html-table


【解决方案1】:
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
| John Poul       |              1 |
| Sanjay          |              1 |
+-----------------+----------------+

假设这是一张桌子tcount_tbl 还有我的另一张桌子tutorials_tbl

+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|           1 | Learn PHP      | John Poul       | 2007-05-24      |
|           2 | Learn MySQL    | Abdul S         | 2007-05-24      |
|           3 | JAVA Tutorial  | Sanjay          | 2007-05-06      |
+-------------+----------------+-----------------+-----------------+

现在正在执行查询。

SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
    -> FROM tutorials_tbl a, tcount_tbl b
    -> WHERE a.tutorial_author = b.tutorial_author;

这就是我可以加入两个表的方式,因为在你的情况下你没有提到整个列名所以我只是假设并给你一个简单的连接示例。您可以进行进一步的连接,如左连接等,以从 3 个表中获取 Source fetching from 3 tables

【讨论】:

    【解决方案2】:
    <table align="center" border="1" width="100%">
    <tr>
    <th>Rank</th>
    <th>Name</th>
    <th>Kills</th>
    <th>Deaths</th>
    </tr> 
    
       $res=mysql_query("SELECT c.* , p.*,d.* FROM ps_plr c,ps_plr_ids_name p,ps_plr_victms d WHERE c.plrid=p.plrid and c.plrid = d.plrid ");
        while($row=mysql_fetch_array($res))
        {
         ?>
            <tr>
            <td><p><?php echo $row[' Rank']; ?></p></td>
            <td><p><?php echo $row['Name']; ?></p></td>
            <td><p><?php echo $row['Kills']; ?></p></td>
            <td><p><?php echo $row['deaths']; ?></p></td>
            </tr>
            <?php
        }
        ?>
    </table>
    

    试试吧希望这有效..

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 2021-08-10
      相关资源
      最近更新 更多