Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 22/02/2011, 15h37   #1
Membre du Club
 
Avatar de Paniez
 
Inscription : janvier 2003
Messages : 160
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : janvier 2003
Messages : 160
Points : 54
Points : 54
Par défaut PDO +SELECT + LIKE

Bonjour,

J'essaye de passer à PDO pour mes requetes Mysql et j'ai un petit soucis.
Je fais :

Code :
1
2
3
4
5
6
7
8
9
10
 
$ptBDD =new PDO('mysql:host='.$this->host.';dbname='.$this->nomBDD, $this->login, $this->mdp);
$ptBDD->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$prep = $ptBDD->prepare($sql);
 
foreach($_tWhere as $cle=>$val)
	      $res=$prep->bindParam($cle, $val, PDO::PARAM_STR);
 
$prep->execute();
$result = $prep->fetchAll();
Ma requete $sql est :
Code :
1
2
 
SELECT * FROM Connexion WHERE identifiant=':identifiant' AND mdp=':mdp' AND site LIKE ':SERVER_NAME'
et mon tableau est :
Code :
1
2
3
4
 
':identifiant'=>'test'
':mdp'=>'test'
':SERVER_NAME'=>'%localhost%'
Je n'ai pas d'erreur (enfin, d'un point de vue code) et en requete directe, elle me retourne bien mon enregistrement mais pas en PDO.

Bonne journée
Paniez est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/02/2011, 15h41   #2
Modérateur
 
Inscription : septembre 2010
Messages : 7 101
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 101
Points : 8 466
Points : 8 466
ton foreach sert a rien, fais direct

Code :
$prep->execute($_tWhere);
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/02/2011, 15h49   #3
Membre du Club
 
Avatar de Paniez
 
Inscription : janvier 2003
Messages : 160
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : janvier 2003
Messages : 160
Points : 54
Points : 54
Re,

Ca ne change pas grand chose.
J'avais mis ça suite au site de php.net :
Citation:
This is WRONG:
"SELECT * FROM `users` WHERE `firstname` LIKE '%:keyword%'";

The CORRECT solution is to leave clean the placeholder like this:
"SELECT * FROM `users` WHERE `firstname` LIKE :keyword";

And then add the percentages to the php variable where you store the keyword:
$keyword = "%".$keyword."%";

And finally the quotes will be automatically added by PDO when executing the query so you don't have to worry about them.

So the full example would be:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
 
 
<?php
// Get the keyword from query string
$keyword = $_GET['keyword'];
// Prepare the command
$sth = $dbh->prepare('SELECT * FROM `users` WHERE `firstname` LIKE :keyword');
// Put the percentage sing on the keyword
$keyword = "%".$keyword."%";
// Bind the parameter
$sth->bindParam(':keyword', $keyword, PDO::PARAM_STR);
?>
Paniez est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/02/2011, 15h53   #4
Modérateur
 
Inscription : septembre 2010
Messages : 7 101
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 101
Points : 8 466
Points : 8 466
que donne : var_dump($result);
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/02/2011, 15h58   #5
Membre du Club
 
Avatar de Paniez
 
Inscription : janvier 2003
Messages : 160
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : janvier 2003
Messages : 160
Points : 54
Points : 54
Re,

Cela me retourne:
Y-a-t-il un moyen pour avoir la requete complète que PDO envoie à mySQL au moment du execute ?
Paniez est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/02/2011, 16h01   #6
Modérateur
 
Inscription : septembre 2010
Messages : 7 101
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 101
Points : 8 466
Points : 8 466
Citation:
Envoyé par Paniez Voir le message
Re,

Cela me retourne:
Y-a-t-il un moyen pour avoir la requete complète que PDO envoie à mySQL au moment du execute ?
ca veut dire que ta requête ne renvoie pas de résultat, parce que ta requete est fausse :

c'est pas :
Code :
SELECT * FROM Connexion WHERE identifiant=':identifiant' AND mdp=':mdp' AND site LIKE ':SERVER_NAME'

c'est :
Code :
SELECT * FROM Connexion WHERE identifiant=:identifiant AND mdp=:mdp AND site LIKE :SERVER_NAME
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/02/2011, 16h17   #7
Membre du Club
 
Avatar de Paniez
 
Inscription : janvier 2003
Messages : 160
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : janvier 2003
Messages : 160
Points : 54
Points : 54
Re,
Citation:
Envoyé par stealth35 Voir le message
Code :
SELECT * FROM Connexion WHERE identifiant=:identifiant AND mdp=:mdp AND site LIKE :SERVER_NAME
Bien vu.
Paniez est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 11h23.


 
 
 
 
Partenaires

Hébergement Web