2

I have a database that contains things like "title, "color", and so on, and I want to display these informations in a webpage. But, for some unknown reason, nothing won't appear, not even an error, which makes me totally lost.

I use this to put everything I need into an array :

<?php 
try{
$req = $db ->prepare("SELECT titre, couleur, categorie, img_url, prix, type FROM articles WHERE id = 9");
$article = $req ->fetch(PDO::FETCH_ASSOC);

}
catch(PDOException $e){
echo $e;
exit();
}
?>

My connection to the database is successful, but I can't even see my array's content even if I use the

<pre> <?php print_r($array) ?> </pre>

technique. I'm basing my current code from a working one, and I can't notice what I did wrong... Thank you in advance !

Barkermn01
  • 6,781
  • 33
  • 83
Jaeger
  • 1,686
  • 1
  • 30
  • 50

1 Answers1

5

How about some executing between the preparation and the fetching? like this:

$req->execute();

Also i think you want to print the array where you fetched your data in like this:

<pre> <?php print_r($article) ?> </pre>

So all in all your code should look something like this:

<?php 

    try{
        $req = $db->prepare("SELECT titre, couleur, categorie, img_url, prix, type FROM articles WHERE id = 9");
        $req->execute();
        $article = $req ->fetch(PDO::FETCH_ASSOC);

    } catch(PDOException $e) { 
        $e->getMessage();

    }

    echo "<pre>";
    print_r($article);
    echo "</pre>";

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • That was one huge mistake ! Thank you for that ! Still it doesn't show anything with this new code : prepare("SELECT titre, couleur, categorie, img_url, prix, type FROM articles WHERE id = 9"); $article = $req ->fetch(PDO::FETCH_ASSOC); $req -> execute(); } catch(PDOException $e){ echo 'Erreur de connexion à la base de donnée, merci de ressayer plus tard'; exit(); } ?> – Jaeger Dec 18 '14 at 15:28
  • @Jaeger Maybe you want to execute the query first and then fetch the data :D?! – Rizier123 Dec 18 '14 at 15:30
  • @Jaeger Sry made a mistake! It has to be `$req` and not `$db`. Just copy the code again and test it! – Rizier123 Dec 18 '14 at 15:32
  • @jaeger,first change line $db->execute(); to $req->execute.for your understanding, you have to execute first then fetch assoc array , see full documentation http://php.net/manual/en/pdostatement.fetch.php – A l w a y s S u n n y Dec 18 '14 at 15:33
  • Sorry for taking so long to respond, my connection is not the best ever. I actually used $req instead of $db (which showed my an error) however it still doesn't show my anything... – Jaeger Dec 18 '14 at 15:36
  • @Jaeger Put: `$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after your connection an see what error you get – Rizier123 Dec 18 '14 at 15:44
  • Problem solved, maybe I'm too tired for this sh#t but I interverted some elements in my code, i fixed it all and it worked... So tired about my self right now, but thank you guys anyway ! – Jaeger Dec 18 '14 at 15:48