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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.