If I have the following function:
function obtener_id(){
$conn = db_connect();
$query = "SELECT max(idproblema) FROM problemas";
$result = @$conn->query($query);
return $result;
}
what I want to do is that what this query returns to me is the number marked in the image, already being an INT... for example, that $result is only 1, I do this query to find out the last id of the table.
I want the result to stay as a number, I don't want it like this:
I would write a function that is safe , like this:
In it I have contemplated the following logic:
0
to the variable$maximo
maximo
to the resulting column ofSELECT
, so that I can later retrieve it usingfetch_assoc
(this is a matter of preferences, code clarity... other methods are possible).$maximo
sea0
if no data is found, be the value of the returned column.$maximo
, as an integer. You can test it usingvar_dump($maximo);
.NOTE: I have removed
@
. It is bad programming practice to silence errors.I hope it helps you.