Querying 6 tables unable to echo info from master table
I am trying to query 6 separate tables in my mysql database, the structures are as follows;
item
itemitemid | item | description | brand | date | time | path |
actor
actoractorid | name | actorthumb | bio |
brand
brandbrandid | brandname | description | image |
movie
moviemovieid | title | genre | year | moviethumb | synopsis|
users
userid | name | surname | email | password |
request
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate |
Using the following query I can display for example where the requestid=1 I can see the movie in the request, the actor in the movie, the item of clothing they were wearing and its brand.
$requestid = mysql_real_escape_string($_GET['requestid']);
$query = "select r.requestid, m.*, a.*, i.*, b.*
FROM request r INNER JOIN movie m ON m.movieid = r.movieid
开发者_StackOverflow社区 INNER JOIN actor a ON a.actorid = r.actorid
INNER JOIN item i ON i.itemid = r.itemid
INNER JOIN brand b ON b.brandid = r.brandid
WHERE r.requestid = $requestid";
However when I try to echo out "content" and "request date" from the request table. The data simply doesn't appear. I also cannot get the info from the user table, e.g the user logging the request by by adding the following join; $query = "select r.requestid, m., a., i., b., u.* INNER JOIN users u ON u.userid = r.userid
Please advise?
You aren't SELECT
ing those fields. Right now, you're only SELECT
ing r.requestid
from the requests
table. You need to add references to every field you want to echo.
As far as the User join, you just seem to be joining on the wrong field. You need to join on u.userid = r.userid
. Right now, you're joining on u.itemid
which doesn't exist. You'll also need to change your SELECT
statement to report the fields you want (e.g. SELECT ... , u.name, u.email
).
As an aside, you should avoid SELECT
ing table.*
where possible. This can break things when you add a field to a table but don't account for that when processing the results of a query. You should try to be explicit as possible, and SELECT
only the fields you want to use - e.g. SELECT users.name, users.email
rather than doing SELECT users.*
.
精彩评论