性能mySql PHP - 关闭一周的几天

时间:2012-01-27 14:44:33

标签: php mysql phpmyadmin

运行此方法以检查业务关闭的一周中的几天最快的方法是什么?

$closingDaysCheck = mysql_query("SELECT * FROM businessClosingDays WHERE Bid='$Bid' LIMIT 1", $con);

if($closingDaysCheck) {
    if(mysql_num_rows($closingDaysCheck) >0) {
        while ($closed = mysql_fetch_assoc($closingDaysCheck)) {
            if((date("w", $finalDate) == 0) && ($closed[0] != 0)) { // SUNDAY
                $active = 'inactive';
            } else if((date("w", $finalDate) == 6) && ($closed[6] != 0)) { // SATURDAY
                $active = 'inactive';
            } else if((date("w", $finalDate) == 5) && ($closed[5] != 0)) { // FRIDAY
                $active = 'inactive';
            } else if((date("w", $finalDate) == 4) && ($closed[4] != 0)) { // THRUSDAY
                $active = 'inactive';
            } else if((date("w", $finalDate) == 3) && ($closed[3] != 0)) { // WEDNESDAY
                $active = 'inactive';
            } else if((date("w", $finalDate) == 2) && ($closed[2] != 0)) { // TUESDAY
                $active = 'inactive';
            } else if((date("w", $finalDate) == 1) && ($closed[1] != 0)) { // MONDAY
                $active = 'inactive';
            } else {
                $active = 'active';
            }
        }
    }
} 

这是数据库,最后一天是星期日,它已关闭:

CREATE TABLE `businessClosingDays` (
  `Bid` varchar(40) NOT NULL,
  `1` tinyint(1) NOT NULL,
  `2` tinyint(1) NOT NULL,
  `3` tinyint(1) NOT NULL,
  `4` tinyint(1) NOT NULL,
  `5` tinyint(1) NOT NULL,
  `6` tinyint(1) NOT NULL,
  `0` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `businessClosingDays` VALUES('9', 0, 0, 0, 0, 0, 0, 1);
帕特里克,我按照你的建议测试了它。 最终我需要它来检查一个月内的所有日子。 我测试了1天,应该关闭。 我从mysql创建一个数组。 然后我检查那个日期的星期几是否在数组中,即。但由于某种原因不起作用。 我做错了什么?

$dateToCheck = 1327791600; // timestamp

$result = mysql_query("SELECT * FROM businessClosingDays WHERE Bid = '9'");
while ($closedDays = mysql_fetch_array($result, MYSQL_NUM)) {
echo $closedDays[0], $closedDays[1], $closedDays[2], $closedDays[3], $closedDays[4], $closedDays[5], $closedDays[6];
}

if (in_array(date("w", $dateToCheck), $closedDays)) {
echo "in it";
} else {
echo "not";
}

2 个答案:

答案 0 :(得分:2)

现在,我真的不明白你对“最快”的意思,因为它现在可能只是微优化,但你可以在逻辑上更好地组织它。

我会摆脱所有if-else子句。由于您对业务是否在某一天关闭感兴趣,您可以查看当天。

然后代码看起来像这样:

// Remember to escape your queries to prevent SQL-injection!
$query = sprintf (
   'SELECT * 
    FROM businessClosingDays 
    WHERE Bid='%s' 
    LIMIT 1'
   mysql_real_escape_string($Bid, $con)
);
$closingDaysCheck = mysql_query($query, $con);
if($closingDaysCheck) {
    // We only need to calculate this once, not in multiple if-else clauses
    $finalWeekday = date("w", $finalDate);

    // Default is that the store is open, right? And a value of 1 
    //means it's closed:
    $active = 'active';

    // Try to fetch the result. Since we use LIMIT 1, we can have
    // at most 1 row, so get rid of the while-loop.
    $closedAt = mysql_fetch_assoc($closingDaysCheck);

    // Now, see if we have any information of when the store is closed
    // and if we do and the store is closed on this day, change the
    // value to 'inactive'
    if(!empty($closedAt) && $closedAt[$finalWeekday] == 1) {
      $active = 'inactive';
    }
} 

假设: 我认为我们只对商店是否在指定日期开放感兴趣,即$finalDate而不是一系列日期。如果这个假设是正确的,那么finalDate对于有问题的变量来说不是一个好名字。它应该以{{1​​}}或类似的名称命名。

答案 1 :(得分:0)

也许看看MySQL的COUNT function。它可以返回与您的查询匹配的行数。