skip to Main Content

I’m trying to load activities from a database with a specific location. I try to do that with this query:

public function selectAllActivities(){
    $sql = "SELECT * FROM `activities` INNER JOIN `locations` on `activities`.`location_id` = `locations.id`";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
  }

However when I load the website, I get this error:

Column not found: 1054 Unknown column 'locations.id' in 'on clause

2

Answers


  1. Have you tried

     `activities`.`location_id` = `locations`.`id`
    

    Inner joins should read like

    ON table1.column_name = table2.column_name;
    

    You can read more at this URL

    Login or Signup to reply.
  2. That’s a typo, but I can’t explain it in comment because of the backticks.

    This :

    `locations.id`
    

    Is meant to be

    //        v-v------- Notice the backticks
    `locations`.`id`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search