谁能帮我写代码?
我在数据库中有一个名为"Date"的表,字段名称为"mydate",它包含以下数据
2011-02-02 00:00:00
2011-01-02 00:00:00
2010-03-02 00:00:00
2010-01-03 00:00:00
2008-03-03 00:00:00
2008-02-03 00:00:00
然后我创建查询并获取其结果
$result=mysql_query("select DATE(mydate) as Date, subject, update_id, description, image from news ORDER BY Date DESC LIMIT 3")or die (mysql_error());
如您所见,我只从$result中获取 3 个数据。现在,当我到达$result的最后一行时,我将其存储在名为"$_SESSION['val']"的会话中。因此,$_SESSION['val'] 的值是 2010-03-02 00:00:00
之后,我将该会话存储到"$val_date"中并创建一个查询。这是我的代码:
$val_date = $_SESSION['val'];
$result=mysql_query("select DATE(mydate) as Date, subject, update_id, description, image from news WHERE Date < $val_date ORDER BY Date DESC LIMIT 3")or die (mysql_error());
$count = mysql_num_rows($result);
问题是,$count=0 它应该是 $count = 3,因为它将根据查询读取以下数据:
2010-01-03 00:00:00
2008-03-03 00:00:00
2008-02-03 00:00:00
这里可能有什么问题?
查询
可能会失败,因为您忘记将字符串分隔符(单引号)放在 mysql 字符串中的日期。
mysql_query("select DATE(date) as Date, subject, update_id, description, image from news WHERE Date < '$val_date' ORDER BY Date DESC LIMIT 3");
但是你应该使用PDO或mysqli,而不是旧的弃用的mysql扩展。
不确定,但您声明您的列名为mydate,但您写了日期?
试试这个
$result=mysql_query("select DATE(mydate) as Date, subject, update_id, description,
image from news ORDER BY mydate DESC LIMIT 3")or die (mysql_error());