在我的工作中,我们有一个模块/网络应用程序,询问我们的数据库,寻找已经RSVP再次与我们住在一起的学生(我们住宿和餐饮) - 无论是在我们的公寓还是住宿大厅。目前的申请需要很长时间才能运行,我被要求优化,让用户可以选择是否有相关学生是我们公寓的居民(下面是Jardine),或者他们是否住在宿舍里。我有一个基本的if语句,根据html选择菜单中的选择构造查询,然后使用builded sql语句运行查询。出于某种原因,当我实现此更改时 - 不会返回任何结果。任何帮助表示赞赏!我们总共运行3个查询,一个主要查询抓住所有居民,另一个查询“意图离开(ITV)”,仅适用于jardine居民,还有一个适用于所有居民。
try
{
if (isset($_POST['select']))
{
if ($_POST['select'] == 'jardine')
{
$jardine = true; // Neighborhood and Apartments include the Jardine under 'Site_name' column
$query = "SELECT * FROM av_user_hms_PatronAssignments a WHERE NOW() BETWEEN ElementStart AND ElementEnd
AND a.Site_name LIKE '%Neighborhood% AND a.Site_name LIKE '%Apartments%'
ORDER BY Last_Name, First_Name";
}
else
{
$jardine = false; // Everything else is Residence Halls
$query = "SELECT * FROM av_user_hms_PatronAssignments a WHERE NOW() BETWEEN ElementStart AND ElementEnd
AND a.Site_name NOT LIKE '%Neighborhood% AND a.Site_name NOT LIKE '%Apartments%'
ORDER BY Last_Name, First_Name";
}
$hms = new hms_helper();
// Run the query
$hmsRows = $hms->query_a($query);
//see if user has completed RSVP application
$query = "SELECT * FROM rsvp.application WHERE wid = ? AND termID = ?";
$apps = $mysqli->prepare($query) or throwNewException("Prepare error:".$mysqli->error." ".$query);
$term = getCurrentTerm();
$apps->bind_param("ii", $stmtArg,$term);
if ($jardine)
{
// also check Jardine Intent to Vacate
$query = "SELECT * FROM ITV.ITV WHERE WID = ? AND STR_TO_DATE(vacate_date,'%m/%d/%Y') > '".date("Y")."-01-01';";
$itv = $eITV->prepare($query) or throwNewException("Prepare error:".$eITV->error." ".$query);
$itv->bind_param("i", $stmtArg);
}
foreach($hmsRows as $k=>$a)
{
//$lastWID is used to keep track of last wid so that no entry is duplicated
$stmtArg = (int)$a["wid"];
$apps->execute();
$appResult = $apps->get_result();// get the result back
if ($jardine)
{
$itv->execute();
$itvResult = $itv->get_result();
if($itvResult->num_rows > 0) // if they are planning to leave jardine unset them
{
unset($hmsRows[$k]);
}
}
if($appResult->num_rows > 0) // if they have an application unset them
{
unset($hmsRows[$k]);// removes this entry from the array so it won't be displayed.
}
elseif($a['ID_Number'] == $lastWID)// don't show duplicates
{
unset($hmsRows[$k]);
}
$lastWID = $a['ID_Number'];
}
}
}
注意: