AL-zami
AL-zami

Reputation: 9066

join query not fetching any data

i am trying to write my first join query which will help me to join two different tables,one is for students and other is for subjects.This is my first join query and it seems it is not working,can anyone point out the mistakes.Php didn't show any error or worning

enter image description here enter image description here

   try{
     $pdo=new PDO("mysql:host=localhost;dbname=mydb",'root','');
     $sql="SELECT * FROM students LEFT JOIN subjects WHERE students.courseid=subjects.courseid";
     $conn=$pdo->prepare($sql);
      if($conn->execute()){
        $results=$conn->fetchAll();
        print_r($results);
      }
     }catch(PDOException $e){
         echo $e->getMessage();
     }

Upvotes: 3

Views: 81

Answers (2)

Dennis Y. Parygin
Dennis Y. Parygin

Reputation: 423

Replace WHERE with ON in your query.

SELECT * FROM students LEFT JOIN subjects ON students.courseid=subjects.courseid

See Mysql JOIN Syntax

Upvotes: 2

davejal
davejal

Reputation: 6133

Assuming you table name for students is students and you subjects table is subject then I think it should be:

SELECT * FROM students LEFT JOIN subjects on students.courseid=subjects.subjectid

because you have a subjectid in your subjects table and not courseid

Upvotes: 2

Related Questions