NineNine
NineNine

Reputation: 324

Getting no data from query. PDO

Okay, so my problem is that this code returns nothing, atleast it doesn't print anything. The only thing that seems to work is $user_email wich does print the users email.

Code:

        $user_email = $_SESSION['user_email'];

        $query = $db->prepare("SELECT u.username, g.color, g.name FROM `user` u JOIN `group` g ON g.id = u.id WHERE u.email = :email");

        $query->bindParam(':email', $user_email, PDO::PARAM_STR);

        $query->execute();

        $row = $query->fetch();
        $user_username = $row['username'];
        $group_color = $row['color'];
        $group_name = $row['name'];
        print "Values: ".$row['username']." | ".$row['name']." <br>";

The "user" table contains 7 different keys:

id (bigint, primary key, auto_increment), group (int), email (varchar), password (varchar), username(varchar), name (varchar), verified (int).

The "group" table contains 3 different keys:

id (bigint, primary key, auto_increment), color (varchar), name (varchar).

That's all there is inside the tables.

Upvotes: 1

Views: 76

Answers (1)

Phil
Phil

Reputation: 164733

Looks to me like the query should be

SELECT u.username, g.color, g.name
FROM `user` u
INNER JOIN `group` g
    ON u.`group` = g.id
WHERE u.email = :email

Also, as user.group appears to be a foreign key for group.id, it should also be a bigint, not int (foreign keys should be the same type as their target primary key).

Also...

group is a terrible name for both a table and column. Try to steer clear of reserved words. I'd go with user_group for the table and group_id for the column.

Upvotes: 1

Related Questions