ctarimli
ctarimli

Reputation: 322

Select all values from multiple tables

I am new to both mysql and php.

I have two tables which are 'members' and 'points'. Both of them including the column 'username'. I want to select all the values from these two tables where username= $POST[username].

So I wrote this but this is not working.

$username = $_POST['username'];
$sql = $con->prepare("SELECT *.members, *.points FROM members, points WHERE
 username=?");
        $sql->bind_param("s", $username);
        $sql->execute();
        $result = $sql->get_result();
        $row = $result->fetch_assoc();

And I tried this :

$sql = $con->prepare("SELECT * FROM members INNER JOIN points 
ON username.points = username.members WHERE username=?");
        $sql->bind_param("s", $username);
        $sql->execute();
        $result = $sql->get_result();
        $row = $result->fetch_assoc();

And this:

$sql = $con->prepare("SELECT *.points, *.members FROM members INNER JOIN points 
ON username.points = username.members WHERE username=?");
        $sql->bind_param("s", $username);
        $sql->execute();
        $result = $sql->get_result();
        $row = $result->fetch_assoc();

I can't use UNION because the number of columbs are not equel in these tables.

So, Please help me what is wrong with the code? What is the proper way to select all from multiple tables.

Upvotes: 2

Views: 1801

Answers (3)

Vikash Jangid
Vikash Jangid

Reputation: 1

check this SELECT * FROM points,members WHERE points.username="'.$_POST['username'].'" AND members.username="'.$_POST['username'].'"; you can check this query it is very simple.

Upvotes: 0

Aaron
Aaron

Reputation: 57748

You were close with this:

SELECT *.points, *.members 
FROM members 
INNER JOIN points ON username.points = username.members 
WHERE username=?

Try this instead:

SELECT *
FROM members 
INNER JOIN points ON members.username = points.username 
WHERE members.username=?

Upvotes: 1

Fabio
Fabio

Reputation: 23490

Alias are meant to be used to specify to which table those column belong, so you need to prepend table name to your columns

SELECT * FROM members 
INNER JOIN points 
ON points.username = members.username
WHERE points.username = ?

You can otherwise assign an alias to your table while selecting and use them

SELECT * FROM members a 
INNER JOIN points b 
ON a.username = b.username 
WHERE a.username = ?

Upvotes: 1

Related Questions