Home  >  Article  >  Backend Development  >  一段方法,求大神指点上异常

一段方法,求大神指点上异常

WBOY
WBOYOriginal
2016-06-13 13:01:13715browse

一段方法,求大神指点下错误

本帖最后由 ymkacscc20 于 2012-12-13 02:53:59 编辑

function get_employees_by_hierarchy( $_employee_id = 0,$_depth = 0,$_org_array = array() ) {
if ( $this->org_depth < $_depth ) {
$this->org_depth = $_depth;
}
$_depth++;
$_query = "SELECT * FROM employees WHERE ";
if ( !$_employee_id ) {
$_query .= "employee_manager_id IS NULL OR employee_manager_id = 0";
}
else {
$_query .= "employee_manager_id = " . $this->dbh->quoteSmart( $_employee_id );
}
$_result = $this->query( $_query );

while ( $_row = $_result->fetchRow() ) {
$_row['depth'] = $_depth;
array_push( $_org_array, $_row );
$_org_array = $this->get_employees_by_hierarchy(
$_row['employee_manager_id'],
$_depth,
$_org_array
);
}
return $_org_array;
}
?>
这段代码还可以做怎么样的优化啊? 3-5行为什么很重要? 17行为什么很重要? 小弟刚开始学php,求大神的指教哦~


------解决方案--------------------
这段代码唯一可说重要的是 $this->dbh->quoteSmart( $_employee_id )
不过从 quoteSmart 字面上看,只是提供一个转义功能。如果仅仅是转义,那么又显得多余了。因为数字是不需要转义的。如果是字符串的话,在构造出的查询串中又没有将其用引号括起

如果 $this->dbh->quoteSmart( $_employee_id ) 返回的是 $_employee_id 的原值的话
那么 "employee_manager_id = " . $this->dbh->quoteSmart( $_employee_id ) 之后将返回
employee_manager_id=$_employee_id 的全部记录
而下面递归调用时的 $_employee_id = $_row['employee_manager_id']
由于 $_employee_id 并未发生改变,于是就将陷入死循环

所以 $this->dbh->quoteSmart( $_employee_id ) 应该返回一个不同于 $_employee_id 的值
因此他就是至关重要的了
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn