2017-10-19 54 views
2

我試圖從數據庫中獲取最大ID。然而,它返回我的錯誤無法從數據庫中獲取最大ID

undefined index: id in $maxID=$rowsInit["id"]; and $response["maxID"] = $rowsInit["id"];

這是我的代碼

if ($_POST['maxID'] == 0) { 
    $queryInit = "SELECT MAX(id) FROM trade"; 
    try { 
     $stmtInit = $db->prepare($queryInit); 
     $resultInit = $stmtInit->execute(); 
    } catch (PDOException $ex) { 
     $response["success"] = 0; 
     $response["message"] = $ex; 
     die(json_encode($response)); 
    } 

    $rowsInit = $stmtInit->fetchAll(); 

    if ($rowsInit) { 
     $maxID = $rowsInit["id"]; 
     $response["maxID"] = $rowsInit["id"]; 
    } else { 
     $response["success"] = 0; 
     $response["message"] = "No Trade Available!"; 
     die(json_encode($response)); 
    } 
} else { 
    $maxID = $_POST['maxID']; 
} 

有一個在我的交易表中的列呼叫ID。我不知道哪部分是錯的。也許我想念一些部分。

+1

$ queryInit =「SELECT MAX(id)as id FROM trade」; 嘗試更改您的查詢。 –

+0

@scarletwitch我改變了查詢,但仍然收到相同的錯誤 – phoon

+0

你可以顯示'$ response'和'$ rowsInit'嗎? – lighter

回答

2

更改爲

SELECT MAX(id) FROM trade 

SELECT MAX(id) AS id FROM trade 

改變這

$maxID=$rowsInit["id"]; 

$maxID=$rowsInit[0]["id"]; # or $maxID=$rowsInit[0]->id 

據我所知獲取的數據索引與0Check these examples


如果無法添加此print_r($rowsInit);die;旁邊if ($rowsInit) {,並檢查它是如何放置在陣列

+0

你可以添加SELECT MAX(id)AS id FROM trade到你的答案嗎?那麼我可以接受它作爲最終答案 – phoon

+0

@ downvoer請指定一個理由?不要粗魯/ **嫉妒** –

+1

這個答案沒有解釋代碼中的錯誤,爲什麼它不起作用。雖然你的建議確實解決了這個問題,但讀者從這個答案中學到了什麼?他們知道他們可以將問題拋出[所以]並且有人會解決它們。 – axiac

4

別名的函數調用id

$queryInit="SELECT MAX(id) as id FROM trade"; 

您還需要獲取第一行的數據。所以也要提供行索引。嘗試

$rowsInit[0]["id"] 
+0

同樣的錯誤,未定義的索引:第22行和第23行中的id是$ maxID = $ rowsInit [「id」];和$ response [「maxID」] = $ rowsInit [「id」]; – phoon

+1

您還需要獲取第一行的數據。所以也要提供行索引。嘗試'$ rowsInit [0] [「id」]' – GurV

0

查詢寫着:

SELECT MAX(id) FROM trade 

爲什麼你期望有一個名爲列在結果集idSELECT子句中沒有這樣的列。只有在MAX(id)表達及其在ResultSet列被命名爲MAX(id)除非你爲它提供一個alias

SELECT MAX(id) AS id FROM trade 

瞭解更多關於SELECT statement的語法。

$rowsInit仍然沒有由於一個id指數:

$rowsInit = $stmtInit->fetchAll(); 

PDOStatement::fetchAll()返回行的陣列。您應該使用$rowsInit = $stmtInit->fetchAll()[0];,或者甚至更好,使用PDOStatement::fetch()只得到第一行(結果集包含的具體哪一行,反正):

$rowsInit = $stmtInit->fetch(); 
1

PDO是遠遠超過大家正在它。除了omniprescent fetchAll()之外,它還有一些方法可以獲得十幾種其他格式的結果,包括單個標量值,因此您不必像其他答案中所建議的那樣更改查詢。所以實際上你只需要一個一行獲得最大ID:

$id = $pdo->query("SELECT MAX(id) FROM trade")->fetchColumn(); 

請注意,您不必如果在它沒有佔位符準備的查詢。

更重要的是,你對error reporting的想法也是錯誤的。你永遠不應該泄漏外面的實際系統錯誤信息。只應該返回一個通用的錯誤信息。

try { 
    if ($_POST['maxID'] == 0) { 
     $maxID = $pdo->query("SELECT MAX(id) FROM trade")->fetchColumn(); 
     if (!$rowsInit) { 
      $response["success"] = 0; 
      $response["message"] = "No Trade Available!"; 
      die(json_encode($response)); 
     } 
    } else { 
     $maxID = $_POST['maxID']; 
    } 

    // here goes your code to get the response 

} catch (Exception $ex) { 
    log_error($ex); 
    $response["success"] = 0; 
    $response["message"] = "Server error"; 
    die(json_encode($response)); 
} 

這裏你包裝你的整個代碼在全球try catch塊將處理不僅PDO異常而且任何其他異常可能發生的。