ccc

Base de datos 8: Cómo añadir una función de ejecutar SQL a nuestra clase de la base de datos

// En nuestra clase db_clase.php añadimos esta nueva función
<?php
public function ejecutar($query, $parametros = array()) {
        if(!$this->handler)
            $this->conectar();
        $statement = $this->handler->prepare($query);

        if($statement === false)    {
            $errorInfo = $this->handler->errorInfo();
            throw new SpoonDatabaseException($errorInfo[2]);
        }

        foreach($parametros as $label => $value)
            $statement->bindValue((is_int($label) ? $label + 1 : (string) $label), $value, $this->getType($value));

        $statement->execute();
        $this->rows_afectadas = $statement->rowCount();

        if($statement->errorCode() != 0)     {
            $aError = $statement->errorInfo();
            throw new SpoonDatabaseException($aError[2]);
        }

      return (int) $this->handler->lastInsertId(); // Devuelve el último Id
}

// Ahora ya desde cualquier fichero externo podremos llamar a esta función para ejecutar 

// cualquier insert, update o delete
?>

Si tienes alguna duda o problema en PHP, haz la pregunta en el comentario de esta entrada e intentaré responderte lo antes posible

No hay comentarios:

Publicar un comentario