For a method, please kindly advise on exceptions

Source: Internet
Author: User
For some method, please kindly advise the error. This post was last edited by ymkacscc20 from 2012-12-1302: 53: 59 & lt ;? Phpfunction & nbsp; get_employees_by_hierarchy (& nbsp; $ _ employee_ I)
This post was last edited by ymkacscc20 at 02:53:59 on

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. = "maid is null or maid = 0 ";
}
Else {
$ _ Query. = "maid =". $ this-> dbh-> quoteSmart ($ _ employee_id );
}
$ _ Result = $ this-> query ($ _ query );

While ($ _ row =$ _ result-> fetchRow ()){
$ _ Row ['dest'] = $ _ depth;
Array_push ($ _ org_array, $ _ row );
$ _ Org_array = $ this-> get_employees_by_hierarchy (
$ _ Row ['employee _ manager_id '],
$ _ Depth,
$ _ Org_array
);
}
Return $ _ org_array;
}
?>
How can I optimize this code? What are 3-5 actions important? 17. what is important? The younger brother just started learning php. please kindly advise me ~


------ Solution --------------------
The only thing that can be said about this code is $ this-> dbh-> quoteSmart ($ _ employee_id)
However, quoteSmart provides only an escape function. If it is just an escape, it seems redundant. Because numbers do not need to be escaped. If it is a string, it is not enclosed in quotation marks in the constructed query string.

If $ this-> dbh-> quoteSmart ($ _ employee_id) returns the original value of $ _ employee_id
Then "maid =". $ this-> dbh-> quoteSmart ($ _ employee_id) will return
All records of employee_manager_id = $ _ employee_id
In the following recursive call, $ _ employee_id = $ _ row ['employee _ manager_id ']
Because $ _ employee_id is not changed, it will be in an endless loop.

Therefore, $ this-> dbh-> quoteSmart ($ _ employee_id) should return a value different from $ _ employee_id.
So he is crucial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.