MySQL错误:'在第5行按时间戳desc限制1'排序

时间:2014-07-28 21:37:31

标签: php mysql sql

我正在尝试几天来自Rowland Carson在MRBS日历中运行脚本“showcalendar.php”。我在数据库中遇到了一些错误,但现在却被这些错误所困:

  

“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在第5行的'time by timestamp desc limit 1'附近使用”

我使用MRBS 1.4.11,Apache / 2.2.16(Debian),MySQL客户端版本:5.1.66。 我找到了这个主题:display todays bookings from mysql mrbs calendar 还检查了http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestamp上的一些信息并进行了gogolresearch 但没有进展。请你能帮助我或给我正确方向的建议吗? 非常感谢你的时间和精力:)

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass)

or die(mysql_error());

mysql_select_db("mrbs", $conn)  or die(mysql_error());



$get_entries = "select timestamp

from $bookings

order by timestamp desc

limit 1

"

;

$entries_result = mysql_query($get_entries) or die(mysql_error());

$data_row = mysql_fetch_object($entries_result);

$last_changed = $data_row->timestamp;



$get_rooms = "select *

from $rooms

order by id

"

;

$rooms_result = mysql_query($get_rooms) or die(mysql_error());

$n_rooms = mysql_num_rows($rooms_result);

$n_cols = $n_rooms + 1;



$booking_block = "<TABLE align=center BORDER=1 CELLPADDING=5>\n";

$booking_block .= "<tr><td colspan=".$n_cols.">";

$booking_block .= "Room bookings for week beginning ";

$booking_block .= $startingDateArray['mday']." ";

$booking_block .= $startingDateArray['month']." ";

$booking_block .= $startingDateArray['year']." ";

$booking_block .= "(Last change to any booking was made ";

$booking_block .= $last_changed;

$booking_block .= ")</td></tr>\n";

$booking_block .= "<tr><td align=right>".$startYear."</td>\n";



while ($rooms = mysql_fetch_array($rooms_result, MYSQL_ASSOC))

{

$booking_block .= "<td>";

$booking_block .= $rooms['room_name'];

$booking_block .= "</td>";

}

$booking_block .= "</tr>\n";



for ($day_number = 0; $day_number <= 6; $day_number++)

{

$booking_block .= "<tr>\n";

$booking_block .= "<td align=right valign=top>";

$endOfDay = $theTimestamp + ONE_DAY;

$theDateArray = getdate($theTimestamp);

if ($theDateArray['year'] != $startYear)

{

    $startYear = $theDateArray['year'];

    $booking_block .= $startYear."<br>";

}

$booking_block .= date("D d M", $theTimestamp)."</td>\n";



for ($room_number = 1; $room_number <= $n_rooms; $room_number++)

{

    $get_bookings = "select *

        from $bookings

        where start_time >= $theTimestamp

        and start_time <= $endOfDay

        and room_id = $room_number

        order by start_time

        "

        ;

    $bookings_result = mysql_query($get_bookings) or die(mysql_error());

    $booking_block .= "<td valign=top>";

    if (mysql_num_rows($bookings_result) > 0)

    {

        while ($booking_entry = mysql_fetch_array($bookings_result, MYSQL_ASSOC))

        {

            $booking_block .= date("H:i", $booking_entry['start_time']);

            $booking_block .= "-";

            $booking_block .= date("H:i", $booking_entry['end_time']);

            $booking_block .= " ";

            $booking_block .= $booking_entry['name'];

            $booking_block .= "<br>\n";

        }

    }

    else

    {

        $booking_block .= "<br>";

    }

    $booking_block .= "</td>";

}

$booking_block .= "</tr>\n";

$theTimestamp = $theTimestamp + ONE_DAY;

}



$booking_block .= "<tr><td colspan=".$n_cols." align=right>";

$booking_block .= "This web page rendered at ";

$booking_block .= sprintf("%02d:%02d:%02d",

                        $nowArray['hours'],

                        $nowArray['minutes'],

                        $nowArray['seconds']);

$booking_block .= " on ";

$booking_block .= $nowArray['mday']." ";

$booking_block .= $nowArray['month']." ";

$booking_block .= $nowArray['year']." ";



$booking_block .= "</table>\n";

1 个答案:

答案 0 :(得分:2)

MySQL的错误片段从发生错误的查询后开始。由于您在错误输出中获得order by ...,这意味着错误恰好在该点之前,这可能意味着$bookings未定义,并且您的查询看起来像

SELECT timestamp
FROM
ORDER BY timestamp DESC

请注意缺少表名。 echo $get_entries将确认。