I want to get the maximum value of the id field , but it shows me an error:
Recoverable fatal error: Object of class stdClass could not be converted to string
public function obtenerMax() {
$sql = "SELECT MAX(id) as maximo FROM tabla";
$bd = new ConexionDB();
$stmt = $bd->query($sql);
$row = $stmt->fetch(PDO::FETCH_OBJ);
return $row;
}
I changed the line of the fetch :
$row = $stmt->fetchColumn();
But if I want to get two values from the same select :
$sql = "SELECT MAX(id) as maximo, SUM(valor) as total FROM tabla";
I can't use fetchColumn anymore how do I get those values?
You are returning the result object, not the actual data within the result. Use $row.maximum