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

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

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

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

本帖最后由 ymkacscc20 于 2012-12-13 02:53:59 编辑
<br />
<?php<br />
function get_employees_by_hierarchy( $_employee_id = 0,$_depth = 0,$_org_array = array() ) {<br />
	if ( $this->org_depth < $_depth ) {<br />
        $this->org_depth = $_depth;<br />
	}<br />
    $_depth++;<br />
    $_query = "SELECT * FROM employees WHERE ";<br />
    if ( !$_employee_id ) {<br />
        $_query .= "employee_manager_id IS NULL OR employee_manager_id = 0";<br />
    }<br />
   	else { <br />
       	$_query .= "employee_manager_id = " . $this->dbh->quoteSmart( $_employee_id );<br />
    }<br />
   	$_result = $this->query( $_query );<br />
<br />
   	while ( $_row = $_result->fetchRow() ) {<br />
       	$_row['depth'] = $_depth;<br />
       	array_push( $_org_array, $_row );<br />
       	$_org_array = $this->get_employees_by_hierarchy(<br />
                       $_row['employee_manager_id'],<br />
                       $_depth,<br />
                       $_org_array<br />
                     );<br />
   				}<br />
   return $_org_array;<br />
}<br />
?><br />
这段代码还可以做怎么样的优化啊? 3-5行为什么很重要? 17行为什么很重要? 小弟刚开始学php,求大神的指教哦~<br />
<br />

------解决方案--------------------
这段代码唯一可说重要的是 $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