MySQL的日期返回NULL

时间:2012-09-22 14:28:20

标签: mysql date null

这是我的表结构:

Datum (Timestamp)     |IP   |X (times visited)
2012-09-08 14:09:44    *      10
2012-09-08 13:20:01    *      34

我使用以下方法从mySQL获取数据:

$Darray=array();
$q="SELECT FROM Datum from ips ORDER BY X DESC";
$rs=mysql_query($q) or die(mysql_error());
while($rd=mysql_fetch_object($rs))
{
$Darray[]=$rd->X;
}

但是当我尝试

var_dump($Darray[1]);

我得到了NULL。

我也尝试过使用

SELECT FROM_UNIXTIME(Datum) from ips ORDER BY X DESC

但它不会改变任何东西

2 个答案:

答案 0 :(得分:2)

您将X列放入数组而不是Datum,并且它可能为null,因为您的SQL错误。

// Create array to hold date values
$date_array = array();

// Get all dates from ips table ordered by X column
$q = "SELECT `Datum` FROM `ips` ORDER BY `X` DESC";

// Query mysql
$rs = mysql_query($q) or die(mysql_error());

// Loop through results as PHP objects
while( $rd = mysql_fetch_object($rs) ) {
    // put the Datum value into array
    $date_array[] = $rd->Datum;
}

// Dump the contents of the $date_array
var_dump($date_array);

答案 1 :(得分:1)

你的sql错了,你有两个FROM子句(FROM Datum from ips):

$q="SELECT FROM Datum from ips ORDER BY X DESC";