php - 如何从两个表中获取sql数据并打印到一个?

标签 php html mysql html-table

我是 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,正如我所说的,就像在其他 table 上找到我需要的东西的基础。

<?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();
?>

最佳答案

+-----------------+----------------+
| 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

关于php - 如何从两个表中获取sql数据并打印到一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41628178/

相关文章:

php - get_categories() 按 "term_order"排序?

html - 如何在 cakePhP 站点中拥有纯 HTML(来自 index.html)目录?

html - 文本在按钮内换行

mysql - Orientdb GC overhead limit exceeded/out of memory 错误和性能低下

mysql - 删除重复记录

php - 需要帮助加入第二张 table

php - 如何在 WampServer 中将 PHP 5.5.12 升级到 5.6.12

PHP 搜索账户

HTML 5 缓存 : window. applicationCache.abort();不起作用

mysql - 从 ID 列返回重复 ID 的计数