Search code examples
phppdoslim-3

Why when I try to use PDO::PARAM_STR in Slim3, Slim return an error?


Can somebody explain me why when I try to use PDO::PARAM_STR in my code, SLIM3 returns me an error ?

 $app->add(new \App\Middlewares\BddMiddleware($Cpdo, $Ctwig));

 class BddMiddleware {

    private $pdo;
    private $twig;

    public function __construct($pdo, $twig)
    {
        $this->pdo = $pdo;
        $this->twig = $twig;
    }

    public function __invoke($request, $response, $next){
        $query = $this->pdo->prepare('SELECT * FROM projet WHERE name = :name');
        $query->bindParam(':name', $path, PDO::PARAM_STR);
        $query->execute();
        $projet = $query->fetch();

I do not understand...

This is my container :

$container['pdo'] = function(){
    $pdo = new PDO('mysql:dbname=portfolio;host=localhost','root','', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};

this doesn't work either..

$query->bindParam(':name', $path, $this->pdo(PDO::PARAM_STR));

Solution

  • Without exact error message, I can only guess. Make sure that you add

    use PDO;
    

    on top of BddMiddleware class.

    To be able to view error messages, turn on Slim debug information by changing application configuration to include following values:

    $config = [  
        'settings' => [
            ...
            'debug' => true,
            'displayErrorDetails' => true,
            ...
        ],
    ];