<?php
$stmt = $con->prepare("SELECT id FROM sample WHERE ID = (SELECT MAX(id) FROM sample)");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
foreach ($result as $row2) {
$auto1 = $row2['id'];
if ($auto1 ===' ') {
echo "There is no ID!";
} else {
echo $auto1;
}
}
?>
如果数据库中有值,则会显示最大ID,但是如果我得到&#34的结果,我将删除所有数据以进行尝试; ID上没有ID!&#34;它没有显示..帮助我
答案 0 :(得分:1)
当返回零行时,您当前的代码不会出现错误消息。您将获得此信息的行是:
$result = $stmt->fetchAll();
如果没有返回任何行,fetchAll()
方法将返回FALSE,因此只需检查该值并相应地显示错误消息。
<?php
$stmt = $con->prepare("SELECT id FROM sample WHERE ID = (SELECT MAX(id) FROM sample)");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
if($!result){
echo "No rows found..!";
} else {
foreach ($result as $row2) {
$auto1 = $row2['id'];
if ($auto1 ===' ') {
echo "There is no ID!";
} else {
echo $auto1;
}
}
}
?>