Recoverable fatal error: Object of class PDOStatement

1

Query:

    static public function contarAccesorios(){
        $ejecucion = self::Conexion();
        $sql = "SELECT count(idaccesorio) FROM accesorios;";
        $registro = $ejecucion->query($sql);
        return $registro;
    }

Why do you give me the following error?

Recoverable fatal error: Object of class PDOStatement could not be converted to string

Test:

echo BD::contarAccesorios();
    
asked by omaza1990 12.12.2017 в 12:38
source

2 answers

4

You can not return the result of the query PDOStatement . You have to read the result and show it:

if ($registro->num_rows > 0) {
    while($row = $registro->fetch_assoc()) {
        //echo...;
    }
} else {
    echo "0 results";
}

EDIT: If you only return a number, as is the case, use:

$contador = $registro->fetch();

With several records you could specify the one you want:

$primero = $contador[0];
    
answered by 12.12.2017 / 12:50
source
1

so I see you prepared the query but you did not execute it

$pdo = self::Conexion(); // Asumiendo que te devuelve una conexion PDO.
$sql = "SELECT count(idaccesorio) FROM accesorios;";
$stmt= $pdo->prepare($sql);
$stmt->execute();
$resultado = $stmt->fetchall(); 
var_dump($resultado);

I hope you serve, greetings.

Source: link

    
answered by 12.12.2017 в 13:01