dimanche 22 septembre 2019

How to join tables in PHP

i'm a beginner in PHP and SQL and i'm trying to create a Website about movies. I'd like to display the actors for each movie (in my database, actors.film_id correspond to the films.id). But when i use the code bellow, i have these 2 errors :

Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:\wamp64\www\Cine\index.php on line 38

Error: Call to a member function fetch() on boolean in C:\wamp64\www\Cine\index.php on line 38

(there is a comment "line 38" in the code bellow)

I think that it's not the right method...

Thanks for helping me :)

<?php
    try{
        $bdd = new PDO('mysql:host=localhost;dbname=cinema_db;charset=utf8', 'root', '');
    }
    catch(Exception $e){
        die('Erreur : ' . $e->getMessage());
    }
    $reponse = $bdd->query('SELECT * FROM films');

    while ($donnees = $reponse->fetch())
    {
    ?>
        <img src="<?php echo $donnees['poster'];?>"/>
        <h1><?php echo $donnees['title'];?></h1>
        <p><?php echo $donnees['synopsis'];?></p><br/>

<?php $reponse2 = $bdd->query('SELECT * FROM actors WHERE actors.film_id = films.id');

     while ($donnees2 = $reponse2->fetch()){ ?>
        <p><strong><?php echo $donnees2['name'];?></strong> : <?php echo $donnees2['character_name'];?></p>//line 38
    <?php
    }}
    $reponse->closeCursor();
    ?>




Aucun commentaire:

Enregistrer un commentaire