【问题标题】:get multiple table with same columns data获取具有相同列数据的多个表
【发布时间】:2022-06-10 23:04:06
【问题描述】:

我有三个具有相同列值的表。例如:假设我有三个表“table1”、“table2”、“table3”,每列都有“id em>”、“标题”、“描述”、“类别”、“日期”、“缩略图”、“管理员”。现在我正在尝试从那些 三个 表中获取所有数据。但有一个想法,我想检查一下 if 语句。如果 table1 与 id 不匹配,请检查 table2。如果 table2 与 id 不匹配,请检查 table3 并最后显示数据。请检查我下面的代码,我正在尝试从这三个表中获取数据:

<?php
            include('config/database.php');
            $id=$_GET['single'];
            $query=mysqli_query($conn,"select * from table1, table2, table3 where id='$id'  ");
            while($row=mysqli_fetch_array($query)){
            $title=$row['title'];
            $date=$row['date'];
            $admin=$row['admin'];
            $thumbnail=$row['thumbnail'];
            $description=$row['description'];
            $category=$row['category'];
         }
          ?>

请帮助我使用 if 语句 从这三个表中获取所有数据 如果您发布答案会更好地理解。提前谢谢你。

【问题讨论】:

  • 你想用 if 语句 显示什么?
  • 我已经用不同的方法解决了我的问题。谢谢。

标签: php mysql sql database get


【解决方案1】:

使用 3 个查询中的 UNION

$sql = "
    SELECT * FROM (
        SELECT 1 AS tnum, * FROM table1 WHERE id = ?
        UNION ALL
        SELECT 2 AS tnum, * FROM table2 WHERE id = ?
        UNION ALL
        SELECT 3 AS tnum, * FROM table3 WHERE id = ?
    ) AS x
    ORDER BY tnum
    LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param('iii', $id, $id, $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row) {
    $title = $row['title']
    $date=$row['date'];
    $admin=$row['admin'];
    $thumbnail=$row['thumbnail'];
    $description=$row['description'];
    $category=$row['category'];
}

tnum 列添加到结果中对它们进行排序,因此首选table1 数据,然后是table2,最后是table3

【讨论】:

  • 它不工作。你能检查你的答案并用我的问题代码编辑它吗?谢谢。
  • 您是否遇到任何错误?请参阅stackoverflow.com/questions/22662488/… 了解如何为 mysqli 启用错误报告。
  • 我在您的链接中没有找到好的解决方案。我是网络开发的新手。所以请尽量帮助我,我会很容易理解。
  • 我不明白。您在第一个答案中没有看到mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 吗?这可以报告 mysqli 错误。将其添加到您的脚本中,并告诉我您在尝试运行我的查询时遇到了什么错误。
  • 对不起,我遗漏了一步:$result = $stmt-&gt;get_result(); 试试更新的代码。
【解决方案2】:
<?php

include "config/database.php";
$id = $_GET["single"];

$query1 = "SELECT * from table1 where id='$id'";
$res1 = $db->query($query1);
foreach ($res1 as $row) {
  $id2 = $row["id"];
  if ($id == $id2) {
    $title = $row["title"];
    $date = $row["date"];
    $admin = $row["admin"];
    $thumbnail = $row["thumbnail"];
    $description = $row["description"];
    $category = $row["category"];
    // Then echo data
  } elseif ($id != $id2) {
    $query2 = "SELECT * from table1 where id='$id'";
    $res2 = $db->query($query2);
    foreach ($res2 as $row2) {
        $id3 = $row["id"];
        if ($id == $id3) {
            $title = $row["title"];
            $date = $row["date"];
            $admin = $row["admin"];
            $thumbnail = $row["thumbnail"];
            $description = $row["description"];
            $category = $row["category"];
            // then echo data
        } elseif ($id != $id3) {
            $query3 = "SELECT * from table1 where id='$id'";
            $res3 = $db->query($query2);
            foreach ($res3 as $row3) {
                $id3 = $row3["id"];
                if ($id == $id3) {
                    $title = $row["title"];
                    $date = $row["date"];
                    $admin = $row["admin"];
                    $thumbnail = $row["thumbnail"];
                    $description = $row["description"];
                    $category = $row["category"];
                    // then echo data
                }
            }
        }
    }
}

} ?>

【讨论】:

  • 它不工作....什么都没有显示。请检查您的答案并解决问题。
  • 为什么需要if ($id == $id2)?查询中的WHERE 子句确保它们相等。
  • 所以else if 条件永远不会为真,并且您永远不会查询第二个表。如果第一个查询根本没有返回任何行,则需要查询第二个表。
  • 因为,我正在将 wp-json 数据存储到我的 sql 数据库中。这是一个新闻网站。所以,我需要 if 语句来检查另一个表和列的新数据并将其显示在 single.php
猜你喜欢
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 2018-05-24
  • 2021-02-28
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多