使用循环创建MySQL查询

时间:2012-10-27 14:52:22

标签: php mysql loops

我正在开发一个用于每小时预订资源的系统。例如,按小时或其他方式预订会议室。我几乎已经完成了所有的功能,但是有些东西还远非完美,而且我有一个功能就是让我烦恼。它预订了整整一周的资源。我的代码如下所示,但是对于提高效率的任何帮助都将非常感激。

谢谢!

史蒂夫

function bookweek($week) {
global $deskid, $date, $time, $member_id, $bookingTimes, $day0, $day1, $day2, $day3, $day4, $day5, $day6, $day7, $day8, $day9, $day10, $day11, $day12, $day13;

dbconnect();

switch($week) {
    case 1: 
        $dupecheck = mysql_query("SELECT * FROM booked where deskid='$deskid' AND date>='$day0' AND date <'$day7'");        
        if (mysql_num_rows($dupecheck) == 0) 
        {
            for($j = 0; $j< 7; $j++) 
            {   
                $daynumber=$j;
                $testing = "day".$daynumber;
                $daytotal= $$testing;
            for($i = 1; $i < count($bookingTimes)+1; $i++) 
            {
                $sql="INSERT INTO booked (date, time, deskid, member_id) VALUES ('$daytotal', '$i', '$deskid', '$member_id')";
                    $result = mysql_query($sql);
                    if (!$result) {
                        die('Invalid query: ' . mysql_error());
                    }
            }
            }
        }
        break;
    case 2:
        $dupecheck = mysql_query("SELECT * FROM booked where deskid='$deskid' AND date > '$day6'");         
        if (mysql_num_rows($dupecheck) == 0) 
        {
            for($j = 0; $j< 7; $j++) 
            {   
                $daynumber=$j+7;
                $testing = "day".$daynumber;
                $daytotal= $$testing;
            for($i = 1; $i < count($bookingTimes)+1; $i++) 
            {
                $sql="INSERT INTO booked (date, time, deskid, member_id) VALUES ('$daytotal', '$i', '$deskid', '$member_id')";
                    $result = mysql_query($sql);
                    if (!$result) {
                        die('Invalid query: ' . mysql_error());
                    }
            }
            }
        }
        break;
}
}

2 个答案:

答案 0 :(得分:1)

如果整个sql块用于任何情况,难以维护
提高效率。

function bookweek($week) {
global $deskid, $date, $time, $member_id, $bookingTimes, $day0, $day1, $day2, $day3, $day4, $day5, $day6, $day7, $day8, $day9, $day10, $day11, $day12, $day13;

$queryP = "SELECT * FROM booked where deskid='".$deskid."' AND date>";
dbconnect();
switch($week) {
case 1: 
       $dupecheck = mysql_query($queryP."='".$day0."' AND date <'".$day7."'");      
       break;
case 2:
       $dupecheck = mysql_query($queryP."'".$day6."'");         
       break;
}
    if (mysql_num_rows($dupecheck) == 0) 
[...]     

尝试一下mysqli

现在代码更清晰,很明显可以改进。

$queryP = "SELECT * FROM booked where deskid='".$deskid."' AND date>";
dbconnect();

switch($week) {
case 1: 
       $queryP .= "='".$day0."' AND date <'".$day7."'";
       break;
case 2:
       $queryP .= "'".$day6."'";
       break;
}
       $dupecheck = mysql_query($queryP);          
       if (mysql_num_rows($dupecheck) == 0) 
[...]

让我们来循环。
为了更好地测试错误。我只会改变内循环。

if (mysql_num_rows($dupecheck) == 0) 
  {
         for($j = 0; $j< 7; $j++) 
         {  
          $testing = "day".$j;
          $daytotal= $$testing;
          $sql = "INSERT INTO booked (date, time, deskid, member_id) VALUES ";
          $sqlTest = $sql;
          for($i = 1; $i < count($bookingTimes)+1; $i++) 
            {
              $sql .= "('".$daytotal."', '".$i."', '".$deskid."', '".$member_id."'),";
            }
          if ($sql == $sqlTest) {
                // nothing to be done or die
            } else {
            $sql = rtrim($sql,",");    
            result = mysql_query($sql);
              if (!$result) {
                 die('Invalid query: ' . mysql_error());
              }
           }
         }
 }

答案 1 :(得分:0)

我认为如果你使用prepared statements,你将节省很多MySQL资源。