2016-08-18 66 views
-1

我收到一條通知:試圖獲取if ($resCategory->num_rows > 0)行中非對象的屬性。我可以知道這裏有什麼錯誤嗎?注意:試圖獲取非對象的屬性

<?php 
function categoryParentChildTree($parent = 'L1', $spacing = '', $category_tree_array = '') { 
    global $MySQLi_CON; 
    $parent = $MySQLi_CON->real_escape_string($parent); 
    if (!is_array($category_tree_array)) 
     $category_tree_array = array(); 
    $sqlCategory = "SELECT * FROM users where enrolled_id = $parent ORDER BY enrolled_id ASC"; 
    $resCategory=$MySQLi_CON->query($sqlCategory); 
    if ($resCategory->num_rows > 0) { 
     while($rowCategories = $resCategory->fetch_assoc()) { 
      $category_tree_array[] = array("id" => $rowCategories['enroller_id'], "name" => $spacing . $rowCategories['user_name']); 
      $category_tree_array = categoryParentChildTree($rowCategories['enroller_id'], '&nbsp;&nbsp;&nbsp;&nbsp;'.$spacing . '-&nbsp;', $category_tree_array); 
     } 
    } 
    return $category_tree_array; 
} 
?> 
+0

1.什麼是MYSQLI_CON設置爲 2.我建議在代碼中使用多個換行符,例如單獨的「IFS」從追加新行代碼的其餘 –

+0

你沒有檢查是否存在故障現在正在引起進一步的失敗。您不應該假設數據庫操作成功。 'if($ resCategory === false){die(mysqli_error($ MySQLi_CON)); }'。 – HiDeo

+0

好吧,我在'where子句'中收到未知列'L1'的錯誤。我的L1存在,它爲什麼顯示錯誤? – stackoverflow

回答

0

你需要用單引號像這樣和它也是一個好主意來包裝在反引號的表名和列名TEXT參數

你也應該進入檢查這樣

$sqlCategory = "SELECT * FROM `users` where `enrolled_id` = '$parent' 
       ORDER BY `enrolled_id` ASC"; 
$resCategory=$MySQLi_CON->query($sqlCategory); 
if (!$resCategory) { 
    echo $MySQLi_CON->error; 
    exit; 
} 

所有API調用的狀態的習慣,從SQL注入攻擊,你也應該使用準備,參數化查詢作爲我們安全LL

$sqlCategory = "SELECT * FROM `users` where `enrolled_id` = ? 
       ORDER BY `enrolled_id` ASC"; 

$stmt = $MySQLi_CON->prepare($sqlCategory); 
if (!$stmt) { 
    echo $MySQLi_CON->error; 
    exit; 
} 

$MySQLi_CON->bind_param('s', $parent); 
$status = $MySQLi_CON->execute(); 

if (!$status) { 
    echo $MySQLi_CON->error; 
    exit; 
} 
+0

感謝信息和這項工作完美:) – stackoverflow

0

這意味着$resCategory不是一個對象。如果查詢失敗,結果query方法可能是一個對象(您期望)或false

看來由於某種原因您的查詢失敗。檢查錯誤的更多細節。

對於mysqli,最後一個錯誤保存在$MySQLi_CON->error;

我可以看到你有SQL語法錯誤,它缺少$parent字符串的引號。

它應該是:

$sqlCategory = "SELECT * FROM users where enrolled_id = '$parent' ORDER BY enrolled_id ASC"; 
+0

ok if($ resCategory = == false){die(mysqli_error($ MySQLi_CON)); },我得到錯誤,像'where子句'中的未知列'L1' – stackoverflow

+0

您必須將字符串值放在引號中。 –

相關問題