php mysql时间预订

时间:2013-01-25 09:06:06

标签: php mysql

我正忙于预订系统,需要检查是否有2次重叠。这是我到目前为止的查询,如果结果是> 1然后在那段时间已经预订了。我在尝试获取行数时收到错误/警告。 (见下文)因此,我的计算不计算,并进行预订。请帮忙。另外,我在我的数据库中使用时间类型(starttime和endttime)但是post变量是字符串时间,因此我使用了strtotime()。请参阅以下错误:

$mystarttime = '07:00:00';
$myendtime = '08:00:00';
$sql    = "SELECT * FROM $tbl_name2 WHERE
            (
            (strtotime($mystarttime) < starttime AND strtotime($myendtime) > starttime)
            OR
            (strtotime($mystarttime) > starttime AND strtotime($myendtime) < endtime)
            OR
            (strtotime($mystarttime) > starttime AND strtotime($mystarttime) < endtime)
            OR
            (starttime < strtotime($mystarttime)  AND  starttime > strtotime($myendtime))
            OR
            (starttime > strtotime($mystarttime) AND endtime < strtotime($myendtime))
            OR
            (starttime > strtotime($mystarttime) AND endtime < strtotime($mystarttime))
            OR
            (strtotime($mystarttime) = starttime)
            OR
            (strtotime($mystarttime) = endtime)
            OR
            (strtotime($myendtime) = starttime)
            OR
            (strtotime($myendtime) = endtime)
            OR
            (starttime = strtotime($mystarttime))
            OR
            (starttime = strtotime($myendtime))
            OR
            (endtime = strtotime($mystarttime))
            OR
            (endtime = strtotime($myendtime))
            )
            AND
            hairdresserid = $myhairdresserid 
            AND
            date = $mydate";

            $result = mysql_query($sql);

            //Mysql_num_row is counting table row
            $count  = mysql_num_rows($result)or die('Query failed: ' . mysql_error() . $sql); 

我收到警告信息:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /xxx/xxx/xxx/xxx/makebooking.php on line 165

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':00:00) < starttime AND strtotime(08:00:00) > starttime) OR ' at line 3SELECT * FROM bookings WHERE ( (strtotime(07:00:00) < starttime AND strtotime(08:00:00) > starttime) OR (strtotime(07:00:00) > starttime AND strtotime(08:00:00) < endtime) OR (strtotime(07:00:00) > starttime AND strtotime(07:00:00) < endtime) OR (starttime < strtotime(07:00:00) AND starttime > strtotime(08:00:00)) OR (starttime > strtotime(07:00:00) AND endtime < strtotime(08:00:00)) OR (starttime > strtotime(07:00:00) AND endtime < strtotime(07:00:00)) OR (strtotime(07:00:00) = starttime) OR (strtotime(07:00:00) = endtime) OR (strtotime(08:00:00) = starttime) OR (strtotime(08:00:00) = endtime) OR (starttime = strtotime(07:00:00)) OR (starttime = strtotime(08:00:00)) OR (endtime = strtotime(07:00:00)) OR (endtime = strtotime(08:00:00)) ) AND hairdresserid = 12 AND date = 2012-01-25

编辑#1:

Query failed: SELECT * FROM bookings WHERE ( (1359118800 < starttime AND 1359122400 > starttime) OR (1359118800 > starttime AND 1359122400 < endtime) OR (1359118800 > starttime AND 1359118800 < endtime) OR (starttime < 1359118800 AND starttime > 1359122400) OR (starttime > 1359118800 AND endtime < 1359122400) OR (starttime > 1359118800 AND endtime < 1359118800) OR (1359118800 = starttime) OR (1359118800 = endtime) OR (1359122400 = starttime) OR (1359122400 = endtime) OR (starttime = 1359118800) OR (starttime = 1359122400) OR (endtime = 1359118800) OR (endtime = 1359122400) ) AND hairdresserid = 12 AND date = 2012-01-25

编辑#2:

Query failed: SELECT * FROM bookings WHERE ( (1359118800 < starttime AND 1359122400 > starttime) OR (1359118800 > starttime AND 1359122400 < endtime) OR (1359118800 > starttime AND 1359118800 < endtime) OR (starttime < 1359118800 AND starttime > 1359122400) OR (starttime > 1359118800 AND endtime < 1359122400) OR (starttime > 1359118800 AND endtime < 1359118800) OR (1359118800 = starttime) OR (1359118800 = endtime) OR (1359122400 = starttime) OR (1359122400 = endtime) OR (starttime = 1359118800) OR (starttime = 1359122400) OR (endtime = 1359118800) OR (endtime = 1359122400) ) AND hairdresserid = 12 AND date = 2012-01-25

编辑#3:

Query failed: SELECT * FROM bookings WHERE ( ('07:00:00' < starttime AND '08:00:00' > starttime) OR ('07:00:00' > starttime AND '08:00:00' < endtime) OR ('07:00:00' > starttime AND '07:00:00' < endtime) OR (starttime < '07:00:00' AND starttime > '08:00:00') OR (starttime > '07:00:00' AND endtime < '08:00:00') OR (starttime > '07:00:00' AND endtime < '07:00:00') OR ('07:00:00' = starttime) OR ('07:00:00' = endtime) OR ('08:00:00' = starttime) OR ('08:00:00' = endtime) OR (starttime = '07:00:00') OR (starttime = '08:00:00') OR (endtime = '07:00:00') OR (endtime = '08:00:00') ) AND hairdresserid = 12 AND date = 2012-01-25

编辑#4:

Query failed: SELECT * FROM bookings WHERE ( ('07:00:00' < starttime AND '08:00:00' > starttime) OR ('07:00:00' > starttime AND '08:00:00' < endtime) OR ('07:00:00' > starttime AND '07:00:00' < endtime) OR (starttime < '07:00:00' AND starttime > '08:00:00') OR (starttime > '07:00:00' AND endtime < '08:00:00') OR (starttime > '07:00:00' AND endtime < '07:00:00') OR ('07:00:00' = starttime) OR ('07:00:00' = endtime) OR ('08:00:00' = starttime) OR ('08:00:00' = endtime) OR (starttime = '07:00:00') OR (starttime = '08:00:00') OR (endtime = '07:00:00') OR (endtime = '08:00:00') ) AND hairdresserid = 12 AND date = '2012-01-25'

2 个答案:

答案 0 :(得分:2)

mysql中没有strtotime函数,因此您必须执行以下操作

从查询中删除所有strtotime并将其添加到$mystarttime$mystarttime = strtotime('07:00:00');) 或者用strtotime($myendtime)替换".strtotime($myendtime)."的每一次出现。

我认为您可以更轻松地编写查询,您可以尝试以下内容:

"SELECT * FROM " . $tbl_name2 . " WHERE 
(starttime BETWEEN " . $mystarttime . " AND " . $myendtime . ") 
AND 
(endtime BETWEEN "    . $mystarttime . " AND " . $myendtime . ") 
AND 
hairdresserid = " . $myhairdresserid . "
AND
date = '" . $mydate . "';"

答案 1 :(得分:1)

strtotime是一个PHP函数。你不能在SQL语句中编写它。相反,连接它。

如果您的字段starttime整数数据类型,我会这样做:

$sql = "SELECT * FROM ".$tbl_name2."
        WHERE startime > '".strtotime($mystarttime)."' 
        AND starttime < '".strtotime($myendtime)."' 
        AND date = '".$mydate."'";  

如果您的字段starttime时间数据类型(例如,00:00:00),我会这样做:

$sql = "SELECT * FROM ".$tbl_name2."
        WHERE startime > '".$mystarttime."' 
        AND starttime < '".$myendtime."' 
        AND date = '".$mydate."'";

关于PHP中的字符串引号,如果连接字符串并在内部使用双引号,则更好的做法是使用单引号。单引号字符串在运行时可能更快,因为它们不需要解析。

$sql = 'SELECT * FROM '.$tbl_name2.'
        WHERE startime > "'.$mystarttime.'"
        AND starttime < "'..$myendtime.'"
        AND date = "'.$mydate.'"';