简体   繁体   English

如何从另一个表中获取一个表的数据?

[英]How to get data of a table from another table?

I have this 2 tables, table1 as the category and table2 as the items in each category, now what i want to do is get the total number of items of table2 where its id is equal to table1.我有这 2 个表,table1 作为类别,table2 作为每个类别中的项目,现在我想要做的是获取 table2 的项目总数,其中它的 id 等于 table1。

Table1
---------
Cats id = 1
Dogs id = 2
Chickens id = 3

Table2
-------
Mouse hunt = under Cats category (table1)
Mouse hunting = under Cats category (table1)
Dog Whisperer = under Dogs category (table1)
Chicken Pasta = under Chickens category (table1)
Chicken Soup = under Chickens category (table1)
How to make chicken Broth = under Chickens category (table1)
Chicken BBQ = under Chickens category (table1)

as per table2 i have 2 items on Cats category, 1 item on DOgs category, 4 under chicken.根据表 2,我在猫类别中有 2 个项目,在狗类别中有 1 个项目,在鸡下有 4 个项目。

when creating those articles/data i usually get table1 ID and store it as invid on table2 so that i can easily fetch and separate the data among other datas.创建这些文章/数据时,我通常会获取 table1 ID 并将其作为 invid 存储在 table2 上,以便我可以轻松地获取和分离其他数据中的数据。

Now problem is im kind stuck on just getting the total :( here is my code现在问题是我有点卡在得到总数上:(这是我的代码

<?php
session_start();
include_once 'connect.php';

if ($_SESSION['userSession']!=1) {
 header("Location: admin.php");
}

$query = $DBcon->query("SELECT * FROM table1");
$DBcon->close();

?>

<html>
  <head>
  <title>Categories</title>

  </head>
    <body>
    <link rel = "stylesheet" href = "css/main.css">
        <div class="main-head" STYLE="FONT-size:20px;text-align: center; padding-top:20px;">
        <strong STYLE="FONT-size:28px;">Just another blog</strong>
        <BR/><a href="account.php"><img src="img/back.png"/></a>&nbsp;|&nbsp;<a href="logout.php?logout"><img src="img/logout.png"/></a>
        </div>
        <div class="main-body"> 
<h1 align="center">Categories</h1>

    <?php

while($userRow=$query->fetch_array()) {

$cid = $userRow['id'];
$subtotal = $DBcon->query("SELECT * FROM table2 WHERE invid=$cid");
$total =  mysqli_num_rows($subtotal);

echo

 '<table width="100%" border="0" style="color:#fff;border: 5px #fff solid;margin-bottom: 20px;">
  <tr>
    <td rowspan="4" align="center" valign="top">
    <img src="inventory/' . $userRow['file'] . '" width="100" height="200" /></td>
    <td align="left" valign="top">
    <a href="viewitems.php?id='. $userRow['id'] . '"><h2 style="line-height:10px;">' . $userRow['name'] . '</h2> </a>   
    <strong>Category Name:</strong> ' . $userRow['name'] . '
    <br>
    <strong>Totalitems:</strong> ' . $total  . '    
        </td>

</table>';
}
?>

        </div>
    </div>
</body>
</html>

now im getting this error现在我收到这个错误

mysqli::query(): couldn't fetch mysqli

can someone pls help me?有人可以帮我吗?

Thank you谢谢

You close the database connection after the query from table1 on the line $DBcon->close();$DBcon->close();行上从 table1 查询后关闭数据库连接$DBcon->close(); , and later try to call it again with $subtotal = $DBcon->query("SELECT * FROM table2 WHERE invid=$cid"); ,然后尝试再次调用它$subtotal = $DBcon->query("SELECT * FROM table2 WHERE invid=$cid");

Move that line to where you're done with database queries and you should be fine.将该行移动到您完成数据库查询的位置,您应该没问题。

As a sidenote, you might want to use prepared statements and use COUNT() to return the number of rows matching your second query.作为旁注,您可能希望使用准备好的语句并使用 COUNT() 返回与您的第二个查询匹配的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM