You must use ORDER BY
that allows you to sort the records thrown out of your query. In specific how you need the last record entered it is convenient to use ORDER BY id DESC
which means order according to the field id
from highest to lowest, that is, you would select from the last record entered (because you already have LIMIT 1 indicating that you bring only one registration as a result)
Then your sql statement would be:
$resultado = $conexion->query("SELECT * FROM articulos WHERE id = $id LIMIT 1");
and your code with the improvement would be as follows:
function obtener_post_por_id($conexion, $id){
$resultado = $conexion->query("SELECT * FROM articulos WHERE id = $id ORDER BY id DESC LIMIT 1");
$resultado = $resultado->fetchAll();
return ($resultado) ? $resultado : false;
}
Observation: It is not advisable to pass the data directly to your query without having previously "escaped" them correctly to avoid security flaws in your application. You can read more about what I mention here: which is the SQL injection and steps to avoid SQL injection