Bonjour,
j'ai la requête suivante :
SELECT * FROM livres WHERE $critere LIKE '%$mot%' ORDER BY $critere $ordre
et j'aimerais la réécrire en PDO.
Voici comment je l'ai réécris :
1 2 3 4 5
| $req = $bdd->prepare('SELECT * FROM livres WHERE :critere LIKE :mot ORDER BY :critere :ordre');
$req->execute(array(
'mot' => $mot_cle,
'critere' => '%$critere%',
'ordre' => $ordre)); |
Mais quand je fais un test, avec un formulaire d'une autre page, ça n'affiche rien. Et pourtant, j'ai choisi un mot qui se trouve dans la base de données.
Donc, est-ce quelqu'un pourrait m'aider en me disant ce qui ne va pas dans ma requête ?
Merci d'avance.
Si besoin est voici mes deux pages :
d'abord la page du formulaire :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="aspect.css" />
<style type="text/css">
body { background-color: lightyellow; }
div { margin: 100px 0px 50px; height: 320px; }
legend { font-size: 1.8em; }
.en_gras { font-weight: bold; }
hr { margin-top: 20px; width: 90%; }
</style>
</head>
<body>
<div>
<form action="resultat_recherche.php" method="post">
<fieldset>
<legend class="en_gras">Recherche d'un livre</legend>
<hr />
<label for="mot_cle" class="en_gras">Mot(s) à chercher : </label>
<input type="text" id="mot_cle" name="mot_cle" size="30" maxlength="20" /><br /><br />
<label for="critere_recherche" class="en_gras">Critère de recherche : </label>
<select name="critere_recherche" id="critere_recherche">
<option value="titre"> Titre </option>
<option value="auteur"> Auteur </option>
<option value="editeur"> Éditeur </option>
<option value="date_parution"> Date de parution </option>
</select><br /><br />
<p>
<span class="en_gras">Par ordre </span>
<input type="radio" name="ordre" value="asc" checked="checked" id="croissant" />
<label for="croissant">Croissant </label>
<input type="radio" name="ordre" value="desc" id="decroissant" />
<label for="decroissant">Décroissant </label><br /><br /><br />
</p>
<input type="submit" value="Envoyer" />
<input type="reset" value="Effacer" />
</fieldset>
</form>
</div>
<p>
<a href="index.html">Page d'accueil</a><br /><br />
<img src="../images/rechercher.gif" alt="recherche" />
</p>
</body>
</html> |
et ensuite la page pour l'affiche des données :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="aspect.css" />
</head>
<body>
<?php
$numpage_cible = "resultat_recherche";
$numpage_source = $_GET['num_page'];
$mot_cle = utf8_decode(htmlspecialchars($_POST['mot_cle']));
$critere = utf8_decode(htmlspecialchars($_POST['critere_recherche']));
$ordre = utf8_decode(htmlspecialchars($_POST['ordre']));
try
{
$bdd = new PDO('mysql:host=localhost;dbname=bibliotheque', 'root', '');
}
catch (PDOException $e)
{
echo 'La base de données est actuellement indisponible, revenez plus tard !';
}
$req = $bdd->prepare('SELECT * FROM livres WHERE :critere LIKE :mot ORDER BY :critere :ordre');
$req->execute(array(
'mot' => $mot_cle,
'critere' => '%$critere%',
'ordre' => $ordre));
while ($donnees = $req->fetch())
{
echo '<p><strong>' . utf8_encode(htmlspecialchars($donnees['titre'])) . '</strong> : ' . utf8_encode(htmlspecialchars($donnees['auteur'])) . '</p>';
}
$req->closeCursor();
?>
</body>
</html> |
Partager