我试图在变量中存储几列,并在表中显示,但它给出了错误


I tried to store a few columns in variables and display in table but it gives error

我写了查询并获取了所有的行,现在我想在变量中存储一些列,这样我就可以在列表中使用它们,但它不起作用。这是我的php代码。

有20行,所以表中应该有20行!但我觉得我在这里漏掉了一些要点

<?php
$servername = "localhost";
$username = "Bat";
$password = "IamTheBat";
$database = "SpecsCompare";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->mysqli_connect_error) {
die("Connection failed: " . $conn->mysqli_connect_error);
} 
echo "Connected successfully";
$query_test = "
SELECT * 
  FROM prices p
     , items i
     , shops s
     , categories c 
 WHERE p.shopid = s.shopid 
   AND i.categoryid = c.categoryid 
   AND p.itemid = i.itemid 
   AND i.categoryid = 1004 
   AND s.shopid = 5005
";
$result = mysqli_query($conn, $query_test);
$rows = mysqli_fetch_all($result, MYSQLI_BOTH);
$name = $row['itemname'];
$price = $row['price'];
$category = $row['categoryname'];
$shop = $row['shopname'];
$manufacturer = = $row['manufacturername'];
$module = $row['modulename'];
$type = $row['typename'];
/*while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    echo "<br>";
    echo $row['itemname'];
}*/
?>

我的HTML表格如下…

   <section class="list">
        <div id="galaxyTable">
          <table class="table table-hover table-condensed">
          <thead>
              <tr>
              <td>Name</td>
              <td>Price</td>
          </tr>
      </thead>
      <tbody>
        <tr>
            <td><td><?php echo $name ?></td></td>
            <td><td><?php echo $price ?></td></td>
            <td><a href="#" class="button">Details</a></td>
        </tr>
          <?php } ?>
      </tbody>
  </table>
  <div id="galaxydetailedTable" class="hidden">
      <table class="table table-hover table-condensed">
      <thead>
          <tr>
              <td>Name</td>
              <td>Category</td>
              <td>Price</td>
              <td>Shop</td>
              <td>Manufacturer</td>
              <td>Module</td>
              <td>Type</td>
          </tr>
      </thead>
      <tbody>
        <tr>
            <td><?php echo $name ?></td>
            <td><?php echo $category ?></td>
            <td><?php echo $price ?></td>
          <td><?php echo $shop ?></td>
          <td><?php echo $manufacturer ?></td>
          <td><?php echo $module ?></td>
          <td><?php echo $type ?></td>   
        </tr>
      </tbody>
  </table>
  </div>
  </div>
  </section>

这里有个错别字你应该修改一下

$rows = mysqli_fetch_all($result, MYSQLI_BOTH);

$row = mysqli_fetch_all($result, MYSQLI_BOTH);

而且你不需要给所有的变量赋值你可以简单地使用Extract它会把键名赋值为一个值为

的变量

来自手册

extract -从数组中导入变量到当前符号表