我试图从数据库中获取最大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'];
}
我的trade表中有一个列调用id。我不知道哪一部分错了。也许我错过了一部分。
最佳答案
把这个改成
SELECT MAX(id) FROM trade
这
SELECT MAX(id) AS id FROM trade
以及
把这个改成
$maxID=$rowsInit["id"];
这
$maxID=$rowsInit[0]["id"]; # or $maxID=$rowsInit[0]->id
据我所知,获取了用
0
索引的数据。Check these examples如果失败,请在
print_r($rowsInit);die;
旁边添加此if ($rowsInit) {
,并检查它在数组中的位置