Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从另一个表中获取一个表的数据?_Php_Mysql - Fatal编程技术网

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

Php 如何从另一个表中获取一个表的数据?,php,mysql,Php,Mysql,我有这两个表,表1作为类别,表2作为每个类别中的项目,现在我想做的是得到表2的项目总数,其中它的id等于表1 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

我有这两个表,表1作为类别,表2作为每个类别中的项目,现在我想做的是得到表2的项目总数,其中它的id等于表1

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)
根据表2,我有2个猫类项目,1个狗类项目,4个鸡类项目

在创建这些文章/数据时,我通常会获取table1 ID,并将其作为invid存储在table2上,这样我就可以轻松地获取数据并将其与其他数据分离

现在的问题是,我有点困在仅仅得到总数:(这是我的代码

<?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>
有人能帮我吗


谢谢

您在查询完表1中的行
$DBcon->close();
后关闭数据库连接,然后尝试使用
$subtotal=$DBcon->query(“SELECT*from table2,其中invid=$cid”);

将该行移到完成数据库查询的位置,您应该会没事的


作为旁注,您可能希望使用和使用COUNT()返回与第二个查询匹配的行数。

注意:
mysqli
的面向对象接口明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的
mysql\u查询
接口混淆。在您对过程式风格投入过多之前,值得切换例如:
$db=newmysqli(…)
$db->prepare(“…”)
过程接口是PHP 4时代的产物,当时引入了
mysqli
API,不应在新代码中使用。警告:使用
mysqli
时,应使用和将用户数据添加到查询中。不要使用字符串插值或连接来完成此操作,因为您创建了严重的.N如果将
$\u POST
$\u GET
或任何用户数据直接放入查询中,如果有人试图利用您的错误进行攻击,可能会非常有害。
mysqli::query(): couldn't fetch mysqli