Reputation: 103
I want to retrieve the last inserted id(id is in auto increment) from the database table named table facility and want to do some calculation(for example I am adding five now) and display the result on the screen. kindly help me I am not able to get this.
<?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;
}
?>
Upvotes: 1
Views: 127
Reputation: 669
Because you did not alias max function to facilityid, so could not call $row->facilityid you should
SELECT MAX(facilityid) As facilityid FROM tblfacility
Also, let use PDO::FETCH_OBJ to get $row as object.
Upvotes: 1
Reputation: 103
<?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;
}
?>
Upvotes: 0