Ok i have two tables with a left join. Let call table one "people" and the second table "dog". I have left joined the tables by an id:
"SELECT * FROM people
LEFT JOIN dog ON people.PK = dog.fk";
My problem is that both tables have a column title "name." When i echo back out the $_POST['name'] variable it give the same value for both fields I tried
echo $_POST['people.name']
echo $_POST['dog.name']
but this isn't returning any data so this must be incorrect. Can someone help me fix my problem without having to change my column names. Thanks you.
The $_POST
superglobal contains data submitted from a form. It does not contain data from database queries. You probably do something like this (pseudocode):
$result = database_query (
"SELECT * FROM people LEFT JOIN dog ON people.PK = dog.fk"
);
The data you want is now contained in the variable named $result
. How you access it will depend on your database access method, but typically you would fetch a row from the result object, perhaps as an associative array.
$row = database_fetch_assoc($result);
Then the array $row
would contain your data.
echo $row['name'];
You might need to alias the column names in the query in order to be able to access the values of both name
columns.
Alias the column names and don't use SELECT *
but specify columns manually:
SELECT people.name AS people_name, dog.name AS dog_name
FROM people
LEFT JOIN dog ON people.PK = dog.fk
Then you can access the fields through ['people_name']
and ['doc_name']
.
Depending on your table structure and which field you actually need you could also SELECT table1.*, table2.somecol AS t2_somecol
- that's handy if you need everything from the first table but only a few fields from the second one.
Oh, and you really shouldn't put stuff into $_POST
. It's meant to be populated by PHP with POST data - and data coming from your DB is not really POST data.