Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

Having issue with MySQL JOIN query (query runs fine in my remote client but error in PHP prepared statement).

I'm getting thrown the following error in my second query:

("SELECT DISTINCT first_name, last_name FROM users INNER JOIN teams ON (teams.league_id = :id) AND (users.id = teams.manager_id");

The error is:

ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

The strange thing is that the query runs fine in Navicat. The only difference is the :id placeholder, but I got no issues with that in the preceding query.

Here's the full function without any html view stuff:

function populateLeague() {
    global $con;
    $leagueID = 1;
    try{
        $stmt = $con->prepare("SELECT team_name FROM teams WHERE league_id = :id");
        $stmt->bindParam(':id', $leagueID);
        $stmt->execute();

        while ($row = $stmt->fetch()) {
            $teamNames[] = $row;
        }
    } catch (PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        return false;
    }
    try{
        $stmt = $con->prepare("SELECT DISTINCT first_name, last_name FROM users INNER JOIN teams ON (teams.league_id = :id) AND (users.id = teams.manager_id");
        $stmt->bindParam(':id', $leagueID);
        $stmt->execute();

    } catch (PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        return false;
    }
}

PS I know global $con isn't best practice but haven't fully grasped one of the solutions yet.

2 Answers

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

Wow, I just caught it.

The original incorrect code:

$stmt = $con->prepare("SELECT DISTINCT first_name, last_name FROM users INNER JOIN teams ON (teams.league_id = :id) AND (users.id = teams.manager_id");

The correct code:

$stmt = $con->prepare("SELECT DISTINCT first_name, last_name FROM users INNER JOIN teams ON (teams.league_id = :id) AND (users.id = teams.manager_id)");

I was simply missing the closing parentheses after manager_id at the end. Amazing what 12 hours away from the computer will do. It was working fine in the client because, of course, the query wasn't wrapped in parentheses.

Joseph Kato
Joseph Kato
35,340 Points

Hi,

Sorry for the late response, but, assuming you're still experiencing this issue, have you tried binding $leagueID as an integer?

For instance:

$stmt->bindParam(':id', $leagueID, PDO::PARAM_INT);
Łukasz Czuliński
Łukasz Czuliński
8,646 Points

Hey, thanks for the reply. Unfortunately I'm still getting the same error message.

The error is: ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1