mysql_fetch_array et PDO

Par sam01, il y a 9 ans


Bonjour,

j'ai préparé la requête suivante :

if($continent != "-1" and $ad_pays != "-1" and $ville != "-1"){ $sqlc = $db->prepare("SELECT MAX(pers) as persmax FROM flatforswap_logement A INNER JOIN flatforswap_adherent B ON A.id_adh = B.id_adh INNER JOIN flatforswap_ville C ON B.id_vil = C.id_vil INNER JOIN flatforswap_pays D ON C.id_pay = D.id_pay INNER JOIN flatforswap_continent E ON D.id_con = E.id_con WHERE E.cont_".$lang."=? and D.pays_".$lang."=? and C.ville_fr=?");} $sqlc->execute(array($continent, $ad_pays, $ville)); $donnees = mysql_fetch_array($sqlc); $persmax = $donnees['persmax']; for($i=1;$i < ($persmax + 1);$i++) { echo '<OPTION value='.$i.'">'.$i.'</option>'; }

mais la ligne suivante ne fonctionne pas :

$donnees = mysql_fetch_array($sqlc);

Merci d'avance pour votre aide.

11 réponses

Carouge10, il y a 9 ans

Bonsoir,

Oui c'est du mysql procédural.
il faut mettre le résultatat de "execute" dans une variable et faire un fetchAll après.

$res = $sqlc->execute(.....); $donnees = $res->fetchAll();
sam01, il y a 9 ans

Bonjour et merci pour ton aide.
Voici ce que j'ai codé

$res = $sqlc->execute(array($continent, $ad_pays, $ville)); $donnees = $res->fetchAll(); $persmax = $donnees['persmax'];

et voici le message d'erreur que j'obtiens :

PHP Fatal error: Call to a member function fetchAll() on a non-object in /home/.....php on line 2510

ça correspond à cette ligne

$donnees = $res->fetchAll();
Carouge10, il y a 9 ans

Oups autant pour moi.

$sqlc->execute(array($continent, $ad_pays, $ville)); $donnees = $sqlc->fetchAll(); $persmax = $donnees['persmax'];
sam01, il y a 9 ans

Cette fois j'ai ça :

Undefined index: persmax in /home/liste.php on line 2511
Carouge10, il y a 9 ans

Que donne un var_dump($donnees); juste après le fetchAll ?

sam01, il y a 9 ans
array(1) { [0]=> array(2) { ["persmax"]=> string(1) "1" [0]=> string(1) "1" } }
Carouge10, il y a 9 ans

Remplacer fetchAll par fetch

sam01, il y a 9 ans

Génial, ça marche !
Un grand merci Carouge10.

Carouge10, il y a 9 ans

Plus qu'à mettre le sujet en résolu

sam01, il y a 9 ans

oui, c'est ce que j'ai cherché à faire mais je ne trouve pas d'item "résolu"....

Carouge10, il y a 9 ans

C'est marqué en vert à coté de chaque post.