Help with return in SELECT

1

$user = $_SESSION["usuario"]["id"];
$sel = BD::conn()->prepare("SELECT lk.*, ep.*
                                    FROM gostados lk
                                    INNER JOIN episodios ep ON 
                                        ep.id_anime = lk.id_anime AND
                                        ep.ep_temporada = lk.ep_temporada AND
                                        ep.episodio = lk.episodio
                                    WHERE 
                                        lk.id_usuario = :user  
                                    ORDER BY data ASC
                                        LIMIT 0,200");

$sel->bindValue(":user", $user);
$sel->execute();
$row = $sel->rowCount();
if($row >= 1){
    echo "1";
}else{
    echo "0";
}

I already checked the id of the user that is being used as SELECT and is correct, what could be the error that causes bindValue to always return 0?

    
asked by anonymous 06.01.2017 / 16:21

1 answer

2

Your problem is probably in the inner join,

Switch to LEft Join and see if it is coming from the other table. Ex:

$user = $_SESSION["usuario"]["id"];
$sel = BD::conn()->prepare("SELECT lk.*, ep.*
                                    FROM gostados lk
                                    LEFT JOIN episodios ep ON 
                                        ep.id_anime = lk.id_anime AND
                                        ep.ep_temporada = lk.ep_temporada AND
                                        ep.episodio = lk.episodio
                                    WHERE 
                                        lk.id_usuario = :user  
                                    ORDER BY data ASC
                                        LIMIT 0,200");

$sel->bindValue(":user", $user);
$sel->execute();
$row = $sel->rowCount();
if($row >= 1){
    echo "1";
}else{
    echo "0";
}

Explanation:

The inner join will only record when it finds the associations of the other table, if you do not have something in the episodes table that connects to the line you want it will not bring, since the left join will bring anyway , provided you have in the first table the "liked"

    
06.01.2017 / 16:35