将MySQL结果填充到网格中



我有一个项目文件表,我选择具有相同日期的项目文件,并在一行中显示它们。例如:

date x --- a b c d
date y ---e f g h
date z----- i j

每行有4个地方保存结果,因为我不期望每天有超过4个项目。我使用下面的查询来获取不同的日期

$sql = "SELECT DISTINCT date FROM project_files WHERE projectNumber = something";
$result = mysql_query($sql) ;

是否有一种简单的方法来获取每一行的数据并显示它?

试试这个:

<html>
<head>
  <title>
    PHP MySQL Test
  </title>
</head>
<body>
<?php
  $db_host = 'localhost';
  $db_user = 'root';
  $db_pwd = 'YOUR_DB_PASSWORD';
  $database = 'YOUR_DB_NAME';
  $table_name = 'project_files';
  if (!mysql_connect($db_host, $db_user, $db_pwd)) {
    die("Can't connect to database");
  }
  if (!mysql_select_db($database)) {
    die("Can't select database");
  }
  $result = mysql_query("SELECT * FROM $table_name") or die("Query to show fields from table failed");
  $fields_num = mysql_num_fields($result);
  echo "<h1>Database: $database</h1>";
  echo "<h2>Table: $table_name</h2>";
  echo "<table border='1'><tr>";
  for($i=0; $i<$fields_num; $i++) {
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
  }
  echo "</tr>n";
  while($row = mysql_fetch_row($result)) {
    echo "<tr>";
    foreach($row as $cell) {
      echo "<td>$cell</td>";
    }
    echo "</tr>n";
  }
  mysql_free_result($result);
?>
</body>
</html>

您没有说明您存储项目文件(a,b,c..)的位置/方式。假设它们是project_files.file....

 SELECT date, GROUP_CONCAT(file) AS list_of_files
 FROM project_files
 WHERE project_number = something  
 GROUP BY date;

(顺便说一句,使用保留字作为列名是一种混乱的做法)。

相关内容

  • 没有找到相关文章

最新更新