Tableau dynamique

Par Amadou Camara, il y a 6 ans


Bonjour,

Voila je rencontre une erreur au niveau de $statement = $pdo->prepare($query);

Ce que je fais

Décrivez ici votre code ou ce que vous cherchez à faire

Entourez votre code pour bien le met<?php use App\NumberHelper; use App\URLHelper; use App\TableHelper; define('PER_PAGE', 20); require 'vendor/autoload.php'; $pdo = new PDO("sqlite:products.db", null, null, [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); $query = "SELECT * FROM products"; $queryCount = "SELECT COUNT(id) as count FROM products"; $params = []; $sortable = ["id", "nom", "city", "price", "address"]; //** Recherche par ville if (!empty($_GET['q'])) { $query .= " WHERE city LIKE :city"; $queryCount .= " WHERE city LIKE :city"; $params['city'] = '%' . $_GET['q'] . '%'; } //** */Organisation if (!empty($_GET['sort']) && in_array($_GET['sort'], $sortable)) { $direction = $_GET['dir'] ?? 'asc'; if (!in_array($direction, ['asc','desc'])) { $direction = 'asc'; } $query .= " ORDER BY " . $_GET['sort'] . "$direction"; } //** Pagination $page = (int)($_GET['p'] ?? 1); $offset = ($page-1) * PER_PAGE; $query .= "LIMIT " . PER_PAGE . " OFFSET $offset"; $statement = $pdo->prepare($query); //Erreur (37) $statement->execute($params); $products = $statement->fetchAll(); $statement = $pdo->prepare($queryCount); $statement->execute($params); $count = (int)$statement->fetch()['count']; $pages = ceil($count / PER_PAGE); ?> <!Doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=devise-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Biens imobiliers</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body class="p-4"> <h1>Les biens immobiliers</h1> <form action="" class="mb-4"> <div class="form-group"> <input type="text" class="form-control" name="q" placeholder="Rechercher par ville" value="<?= htmlentities($_GET['q'] ?? null) ?>"> </div> <button class="btn btn-primary">Rechercher</button> </form> <table class="table table-striped"> <thead> <tr> <th><?= TableHelper::sort('id', 'ID', $_GET) ?></th> <th><?= TableHelper::sort('name', 'Nom', $_GET) ?></th> <th><?= TableHelper::sort('price', 'Prix', $_GET) ?></th> <th><?= TableHelper::sort('city', 'Ville', $_GET) ?></th> <th><?= TableHelper::sort('address', 'Adresse', $_GET) ?></th> </tr> </thead> <tbody> <?php foreach ($products as $product): ?> <tr> <td>#<?= $product['id'] ?></td> <td><?= $product['name'] ?></td> <td><?= NumberHelper::price($product['price']) ?></td> <td><?= $product['city'] ?></td> <td><?= $product['address'] ?></td> </tr> <?php endforeach ?> </tbody> </table> <?php if ($pages > 1 && $page > 1): ?> <a href="?p=<?= URLHelper::withParam($_GET, "p", $page - 1) ?>" class="btn btn-primary">Page précédente</a> <?php endif ?> <?php if ($pages > 1 && $page < $pages): ?> <a href="?p=<?= URLHelper::withParam($_GET, "p", $page + 1) ?>" class="btn btn-primary">Page suivante</a> <?php endif ?> </body> </html>

Ce que je veux

Je cherche de l'aide

Ce que j'obtiens

Décrivez ici vos erreurs ou ce que vous obtenez à la place de ce que vous attendez :(
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 near "20": syntax error in C:\wamp64\www\phpGrafikart\Exemple_avancer\Tableau\index.php on line 37

( ! ) PDOException: SQLSTATE[HY000]: General error: 1 near "20": syntax error in C:\wamp64\www\phpGrafikart\Exemple_avancer\Tableau\index.php on line 37

2 réponses

Azorgh, il y a 6 ans

Hello,

Comme le précise ton erreur, il y a juste une erreur au niveau de ta requête SQL.
Il faudrait que tu debug $query juste avant la requête, et vérifier le contenu de celle-ci et corriger ce qu'il ne va pas.

Ulrich Nelson, il y a 6 ans

bonjour il faut un peu regarder cette ligne $query .= "LIMIT " . PER_PAGE . " OFFSET $offset"; ce qu'il faut faire est de laisser un espace entre les guillemets et LIMIT comme ceci ($query .= " LIMIT " . PER_PAGE . " OFFSET $offset";) et voir si cela fonctionne ou pas.