一段方法,求大神指點上異常

來源:互聯網
上載者:User
一段方法,求大神指點下錯誤
本帖最後由 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 的值
因此他就是至關重要的了
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.