【问题标题】:Which of these 2 PHP & MySQL methods for displaying credits from a table is more logical to use?这 2 种 PHP 和 MySQL 方法中哪一种更合乎逻辑?
【发布时间】:2020-07-07 19:15:59
【问题描述】:

我正在开发一个使用 PHP 和 MySQL 来显示学分的网站。我已经制作了相同东西的 2 个版本,但我很难决定哪一个是最好用的。一个使用 2 个表格,另一个只使用 1 个。我设置了一些东西,其中列出了职位的名称,然后将人员放在其下方。我无法决定是否更容易查看credits_position 表然后从credits_people 获取所需的内容,或者我是否应该只使用credits_people 来保存所有内容。哪个版本更容易保持更新?

SQL

CREATE TABLE `credits__topic` (
 `topic_id` INT(11) NOT NULL AUTO_INCREMENT,
 `category_id` INT(11) DEFAULT NULL,
 `name` VARCHAR(48) DEFAULT NULL,
 `category` VARCHAR(48) DEFAULT NULL COMMENT 'Job name',
 PRIMARY KEY (`topic_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 26;
CREATE TABLE `credits__category` (
 `category_id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(48) DEFAULT NULL COMMENT 'Job name',
 PRIMARY KEY (`category_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 6;
INSERT INTO `credits_people` (`topic_id`, `category_id`, `name`, `category`)
VALUES 
(1, 1, 'Matthew Campbell', 'Creator'),
(2, 2, 'Godzilla', 'Assistant'),
(3, 1, 'Billy Bob', 'Creator'),
(4, 4, 'Martha Stewart', 'Tester'),
(5, 2, 'Mothra', 'Designer'),
(6, 2, 'Rodan', 'Contributing'),
(7, 2, 'King Ghidorah', 'Designer'),
(8, 3, 'Mechagodzilla', 'Assistant');
INSERT INTO `credits_position` (`category_id`, `name`)
VALUES
(1, 'Creator'),
(2, 'Designer'),
(3, 'Assistant'),
(4, 'Tester'),
(5, 'Contributing');

PHP

<?php
 $connection = mysqli_connect ("localhost", "root", "root", "main");

 // Version 1:
 $data = mysqli_query ($connection, "SELECT `mc`.`name` AS 'category_name', `mt`.`name` AS 'topic_name', `mt`.`category` AS 'topic_category' FROM `credits_position` AS `mc` INNER JOIN `credits_people` AS `mt` USING (`category_id`);");
 // Version 2:
 $data = mysqli_query ($connection, "SELECT `name` AS 'category_name', `category` AS 'topic_category' FROM `credits_people`;");

 // Works with both versions
 $responses = array ();
 while ($row = mysqli_fetch_array ($data)) {
  array_push ($responses, $row);
 }
 // This loads the 1st category name.
 $category = $responses [0] ["category_name"];
 echo "<p>" . $category . "</p>\n";
 echo "<ol>\n";
 foreach ($responses as $response) {
  // This loads when it finds that the category is different from the previous loop.
  if ($category !== $response ["category_name"]) {
   $category = $response ["category_name"];
   echo "</ol>\n";
   echo "<p>" . $response ["category_name"] . "</p>\n";
   echo "<ol>\n";
  }
  echo "<li>" . $response ["topic_name"] . "</li>\n";
 }
 echo "</ol>";

 mysqli_close ($connection);
?>

【问题讨论】:

    标签: php mysql sql credits


    【解决方案1】:

    在您希望节省空间的大型数据库上,将其用于相关数据。

    所以一个规范化的表,你只有 category_id(当然还有更多这样的表),你可以节省很多空间。

    但我怀疑您将拥有数百万个类别,因此您应该从小处着手,如果需要,您可以增加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多