Я предоставил полный код PDO для вашего текущего сценария, который вставляет данные в таблицу и затем получает информацию о последней вставленной записи.Для лучшего понимания вам следует изучить PHP PDO Class .Это действительно просто, легко, и вы можете найти много вещей на эту тему.
<?php
//Specifying database credentials
$dbhost = "localhost";// Your host name
$dbname = "test"; // Your database name
$dbuser = "root"; // database user
$dbpass = "";// Database password
$Name = $_POST["Name"];
$Age = $_POST["Age"];
$RecordDate = date("d-M-Y h:i:s a");
$jTableResult = array();
//Establishing connection to the database
try
{
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
die();
}
$sql = "INSERT INTO people(Name, Age, RecordDate) VALUES (:Name, :Age, :RecordDate)";
$statement = $db->prepare($sql);
//Bindging values to be added to People table
$statement->bindParam(":name", $firstname );
$statement->bindParam(":Age", $Age );
$statement->bindParam(":RecordDate", $RecordDate );
if($statement->execute()) // Check if insert statement executed successfully
{
$PersonId = $db->lastInsertId(); // Getting Last inserted id;
$get_statement= $db->prepare("SELECT * FROM people WHERE PersonId=:PersonId");
$get_statement->bindParam(":PersonId", $PersonId );
$get_statement->execute(array(':Uemail'=>$email, ':Upassword'=>$password));
if($row=$get_statement->fetch(PDO::FETCH_ASSOC)) // as we are getting only one record, therefore fetch() method is best. The fetchAll should be used if you are getting multiple records
{
$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $row;
print json_encode($jTableResult);
}
else
{
echo json_encode("Error");
}
}
else
{
$jTableResult['Result'] = "FAIL";
$jTableResult['Record'] = array();
print json_encode($jTableResult);
}
?>