0

I am trying to learn how to use JOIN, INNER JOIN, LEFT JOIN and RIGHT JOIN. I have the following users and generator tables

users
  uid
  username
  password
  email

and

creator
  id
  uid
  g_name
  g_bio

But I cannot join them. I try to JOIN like this :

public function Generator($uid) {
            $g_name=mysql_real_escape_string($uid);
            $query= mysql_query ("SELECT username,g_name,g_bio FROM users JOIN creator ON users.uid = creator.uid");
            $data=mysql_fetch_array($query);

            return $data;
            }

But print_r does not show anything? What is wrong here?

Giacomo1968
  • 24,837
  • 11
  • 67
  • 96
MltHty
  • 37
  • 6

2 Answers2

0

First thing, when you are referencing two tables, you need to specify from which table you are selecting each column from:

SELECT 
    users.username,
    creator.g_name,
    creator.g_bio 
FROM users 
JOIN creator ON users.uid = creator.uid

Second, in your function, you are passing in $uid, and then converting it to $g_name, but not using it anywhere in your query. Assuming you want rows matching this value, you need to add an additional where condition:

WHERE creator.g_name = '$g_name'

Finally, don't use the mysql_*() functions, instead use mysqli or pdo and utilize prepared statements.

Community
  • 1
  • 1
Mark Miller
  • 7,442
  • 2
  • 15
  • 21
0

You escaped function argument $uid to $g_name but you don't use it. May be it the reason of "nothing happened" ? You should rewrite your query that way :

"SELECT username,g_name,g_bio FROM users
    JOIN creator ON users.uid = creator.uid
    WHERE g_name = '$g_name'"

Beyond that I think you better should use PDO and a parameterized queries