Jump to content

Table joins


bob_the _builder

Recommended Posts

Hi,

Thanks that seems to work, I guess it has to be 2 queries?

I dont really understand when you should use left joins or inner joins etc   :(

A query to show a random image for each maincat from a corrosponding random subcat .. I guess thats hard as there is no post data to make the select from?

Also abit off mysql, should you also clean any data sent across the url like &maincat_id=$_POST['maincat_id'], I guess code can be added to the url to cause damage to the site?

also is (list($photo_filename) away around creating a variable $photo_filename = $row['photo_filename'];?


Thanks
Link to comment
https://forums.phpfreaks.com/topic/19430-table-joins/page/2/#findComment-86066
Share on other sites

A left join B

- select all recs from A even if there is no matching rec in B


A inner join B

- select only those recs from A and B that match on the join column{s)


Clean data

Never put data straight into a query that originates form the client. That means any data in GET, POST or COOKIE. Always clean it - as a minimum addslashes, if magic_quotes_gpc is off (if it's on it does it for you). You might also want to strip_tags() to prevent javascript and html code from being entered.
Link to comment
https://forums.phpfreaks.com/topic/19430-table-joins/page/2/#findComment-86069
Share on other sites

Hi,

I got it using:

[code]$preview = mysql_query("SELECT i.photo_filename
        FROM gallery_images i
        INNER JOIN gallery_subcat s
        ON i.subcat_id = s.subcat_id
        INNER JOIN gallery_maincat m
        ON m.maincat_id = s.maincat_id
        WHERE m.maincat_id = {$row['maincat_id']}
ORDER BY RAND()") or die(mysql_error());[/code]

had a query prior the the $preview one for he pagination .. so was able to use $row['maincat_id']

[code] $sql = mysql_query("SELECT * FROM gallery_maincat ORDER BY maincat_id DESC LIMIT $from, $max_results");[/code]

Is this a reasonable way to go about it?


Thanks
Link to comment
https://forums.phpfreaks.com/topic/19430-table-joins/page/2/#findComment-89677
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.