我有一个包含项目文件的表,我正在选择具有相同日期的项目文件并将它们连续显示。例如:
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) ;
是否有一种简单的方法来获取每行的数据并显示它?
答案 0 :(得分:2)
试试这个:
<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>
答案 1 :(得分:0)
您没有说明存储项目文件的位置/方式(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;
(BTW使用保留字作为列名称是一种混乱的做法)。