PHP:从数据库中提取的列中未更新日期(phpMyAdmin)

时间:2014-11-26 20:13:03

标签: php mysql

我是PHP的新手,似乎无法找到在表格中显示正确时间的方法。 时间格式是正确的,但它也显示了while循环中所有后续行中第一行的日期。我认为由于某种原因,变量不会被覆盖。 我应该改变什么才能显示正确的时间?

这是我的代码段。

<?php
// Variables
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "Clothes";
$dberror1 = "Could not connect to database.";
$dberror2 = "Could not select database.";
$dberror3 = "Could not execute query.";


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);

$select_db = mysql_select_db('Clothes') or die ($dberror2);


echo "<table border='1'>
<tr>
<th>Brand</th>
<th>Colour</th>
<th>Fabric</th>
<th>Pattern</th>
<th>PurchaseDate</th>
</tr>";

$query = "SELECT b.Brand, c.Colour, f.Fabric, p.Pattern, d.Date 
FROM TSHIRTS AS t, Brands as b, Fabrics AS f, Patterns AS p,PurchaseDates as d, Colours as c 
WHERE b.ID=t.Brand 
AND c.ID=t.Colour 
AND p.ID=t.Pattern 
AND f.ID=t.Fabric 
AND d.ID=t.PurchaseDate
ORDER BY PATTERN;";


$fetch = mysql_query($query) or die($dberror3);

while($row = mysql_fetch_assoc($fetch))
{
    echo "<tr>";
    echo "<td>".$row['Brand']."</td>";
    echo "<td>".$row['Colour']."</td>";
    echo "<td>".$row['Fabric']."</td>";
    echo "<td>".$row['Pattern']."</td>";
    $date = new DateTime($row['PurchaseDate']);
    echo "<td>".$date->format('Y-m-d')."</td>";
    echo "<br>";
    echo "</tr>";
}

echo "</table>";

$conn->close();
?>

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php
// Variables
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "Clothes";
$dberror1 = "Could not connect to database.";
$dberror2 = "Could not select database.";
$dberror3 = "Could not execute query.";


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);

$select_db = mysql_select_db('Clothes') or die ($dberror2);


echo "<table border='1'>
<tr>
<th>Brand</th>
<th>Colour</th>
<th>Fabric</th>
<th>Pattern</th>
<th>PurchaseDate</th>
</tr>";

$query = "SELECT b.Brand, c.Colour, f.Fabric, p.Pattern, d.Date 
FROM TSHIRTS AS t, Brands as b, Fabrics AS f, Patterns AS p,PurchaseDates, Colours as c 
WHERE b.ID=t.Brand 
AND c.ID=t.Colour 
AND p.ID=t.Pattern 
AND f.ID=t.Fabric 
AND d.ID=t.PurchaseDate
ORDER BY PATTERN;";


$fetch = mysql_query($query) or die($dberror3);

while($row = mysql_fetch_assoc($fetch))
{
    echo "<tr>";
    echo "<td>".$row['Brand']."</td>";
    echo "<td>".$row['Colour']."</td>";
    echo "<td>".$row['Fabric']."</td>";
    echo "<td>".$row['Pattern']."</td>";
    $date = new DateTime($row['PurchaseDates']);
    echo "<td>".$date->format('Y-m-d H:i:sP')."</td>";
    echo "</tr>";
}

echo "</table>";

这样可行。

答案 1 :(得分:0)

你应该使用var_dump($ row);检查你得到了什么,然后你会看到什么是错的。您没有选择名为PurchaseDate的字段。另外,我认为Date是一个保留关键字,所以你应该引用它。

$query = "SELECT b.Brand, c.Colour, f.Fabric, p.Pattern, d.`Date` as PurchaseDate
FROM TSHIRTS AS t, Brands as b, Fabrics AS f, Patterns AS p,PurchaseDates as d, Colours as c 
WHERE b.ID=t.Brand 
AND c.ID=t.Colour 
AND p.ID=t.Pattern 
AND f.ID=t.Fabric 
AND d.ID=t.PurchaseDate
ORDER BY PATTERN;";


$fetch = mysql_query($query) or die($dberror3);

while($row = mysql_fetch_assoc($fetch))
{
    echo "<tr>";
    echo "<td>".$row['Brand']."</td>";
    echo "<td>".$row['Colour']."</td>";
    echo "<td>".$row['Fabric']."</td>";
    echo "<td>".$row['Pattern']."</td>";
    $date = new DateTime($row['PurchaseDate']);
    echo "<td>".$date->format('Y-m-d')."</td>";
    echo "<br>";
    echo "</tr>";
}