【发布时间】:2009-07-03 21:21:33
【问题描述】:
我有一些非常时髦的代码。正如您从下面的代码中看到的那样,我添加了一系列过滤器来查询。现在是否更容易只使用多个查询,使用它自己的一组过滤器,然后将结果存储在一个数组中,还是搞得一团糟?
有没有人有更好的解决方案来解决这个混乱?我需要能够按关键字和项目编号进行过滤,并且需要能够使用多个值进行过滤,不知道哪个是哪个。
//Prepare filters and values
$values = array();
$filters = array();
foreach($item_list as $item){
$filters[] = "ItemNmbr = ?";
$filters[] = "ItemDesc LIKE ?";
$filters[] = "NoteText LIKE ?";
$values[] = $item;
$values[] = '%' . $item . '%';
$values[] = '%' . $item . '%';
}
//Prepare the query
$sql = sprintf(
"SELECT ItemNmbr, ItemDesc, NoteText, Iden, BaseUOM FROM ItemMaster WHERE %s LIMIT 21",
implode(" OR ", $filters)
);
//Set up the types
$types = str_repeat("s", count($filters));
array_unshift($values, $types);
//Execute it
$state = $mysqli->stmt_init();
$state->prepare($sql) or die ("Could not prepare statement:" . $mysqli->error);
call_user_func_array(array($state, "bind_param"), $values);
$state->bind_result($ItemNmbr, $ItemDesc, $NoteText, $Iden, $BaseUOM);
$state->execute() or die ("Could not execute statement");
$state->store_result();
【问题讨论】: