我想从名为table facility的数据库表中检索最后插入的id(id是自动递增的),并想要进行一些计算(例如,我现在要添加五个)并在屏幕上显示结果。请帮助我,我无法得到这个。
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
{
header('location:index.php');
}
else{
$sql="SELECT MAX(facilityid) FROM tblfacility ";
$result=$dbh -> query($sql);
$row=$result->fetch(PDO::FETCH_ASSOC);
echo "<pre>", print_r($row),"</pre>";
echo $result ;
$code = $row->facilityid;
$sampleid= $code +5;
echo $sampleid;
}
?>
答案 0 :(得分:1)
因为您没有将max函数别名为africationid,所以您不能调用$ row-> facilityid
SELECT MAX(facilityid) As facilityid FROM tblfacility
另外,让我们使用PDO :: FETCH_OBJ获取$ row作为对象。
答案 1 :(得分:0)
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
{
header('location:index.php');
}
else{
$sql="SELECT MAX(facilityid) As facilityid FROM tblfacility ";
$result=$dbh -> query($sql);
$row=$result->fetch(PDO::FETCH_ASSOC);
$code=$row[facilityid];
$sampleid= $code +1;
echo $sampleid;
}
?>