Jump to content

Recommended Posts

Hi there,

I'm having problems joining two databases together; I can't seem to pluck data from the second one. I'm new to PHP and if you could highlight what I'm doing wrong and what I'd need to do to correct myself, I'd be very appreciative.

[code]$media = mysql_query('SELECT * FROM files LEFT JOIN albums ON files.album=albums.id WHERE ORDER BY timestamp DESC LIMIT 0, 1');

while($latestmedia = mysql_fetch_array($media))
{
$displaymedia=('
<div id="home-media-image">
<a href="/'.$latestmedia['directory'].'/'.$latestmedia['file'].'" target="image"><img src="/'.$latestmedia['directory'].'/'.$latestmedia['file'].'" border="0" width="114" height="84"></a>
</div>
<a href="?view=image_gallery&album='.$latestmedia['album'].'"><div id="home-media-select">
<div id="home-media-l"></div>
<div id="home-media-text">More for this game</div>
<div id="home-media-r"></div>
</div></a>
');

$bars = "\|";
$removebars = "";

echo ereg_replace($bars, $removebars, $displaymedia);

}[/code]

(The ereg_replace is to remove the "|" symbol from items in that row. The albums come up as "|XXXX|" for example.)

Many thanks!
Link to comment
https://forums.phpfreaks.com/topic/25709-problems-joining-two-tables/
Share on other sites

$media = mysql_query('SELECT * FROM files LEFT JOIN albums ON (files.album=albums.id) ORDER BY timestamp DESC LIMIT 0, 1');

Try that, I usually inclose the join with (). Also your WHERE didn't do anything...this is more then likely your problem.

Let me know how it works out.
-Chris
[quote author=Destruction link=topic=113361.msg460588#msg460588 date=1162310322]
You're only selecting all fields from files, you need to do something like

SELECT * FROM files, albums LEFT JOIN albums ON[/quote]

I have tried this, but I get shouted at by my server:

[quote]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in index.php on line 241[/quote]

FYI, line 241 is:

[code]while($latestmedia = mysql_fetch_array($media))[/code]

My database structure is below:

[b]files[/b]
id (primary)
name
file
album
type
timestamp
size
description

[b]albums[/b]
id (primary)
name
directory

The ones I need are, from files, 'file', 'album', (and 'timestamp' to order) and from albums, 'directory' (as well as 'id' to define the row).


Many thanks!
If you are matching up the album names wouldn't it be:

$media = mysql_query('SELECT * FROM files LEFT JOIN albums ON (files.album=albums.name) ORDER BY timestamp DESC LIMIT 0, 1');

...what you should do is make another key in files (a_id) for album id then it can be (files.a_id=albums.id) which would make it a lot easier
[quote author=cmgmyr link=topic=113361.msg460597#msg460597 date=1162311052]
If you are matching up the album names wouldn't it be:

$media = mysql_query('SELECT * FROM files LEFT JOIN albums ON (files.album=albums.name) ORDER BY timestamp DESC LIMIT 0, 1');
[/quote]

Basically I'm trying to get the file 'album' value to match the albums 'id' value, then return the albums 'directory'; the 'name' column is irrelevant as that's to do with what will be displayed on the actual page for the album (this data is just for the homepage itself).

[quote author=cmgmyr link=topic=113361.msg460597#msg460597 date=1162311052]
...what you should do is make another key in files (a_id) for album id then it can be (files.a_id=albums.id) which would make it a lot easier
[/quote]

I'm doing that for all of my tables, but what I have to work with is this, and I can't really change it (as much as I'd like to) as the site is already running off these databases and don't even want to begin touching any of the existing code, as it's a total mess (hence the new code XD).

There are over 12,000 rows in files, and over 1,500 in albums. :(

There surely must be a way to be able to do this with the existing tables though?
Chris,
It's now displaying the same result as earlier; the latest row, but without the 'directory'! I've also just tried using my n00bish PHP skills to write things like '.$latestmedia['files.file'].' but that just destroyed everything, so I've reverted back to what I had initially...
Sure, here is the latest record:

[quote]id        name    file            album      type      timestamp                  size        description
15311    000      000.JPG      |1763|    image    2006-10-24 15:05:32    34775[/quote]

Which should match with:

[quote]id      name                          directory
1763  Guilty Gear Judgement    Guilty Gear Judgement[/quote]

The HTML I get is:

[code]<a href="downloads//000.JPG" target="image"><img src="downloads//000.JPG" border="0" width="114" height="84"></a>
</div>
<a href="?view=image_gallery&album=1763"><div id="home-media-select">
<div id="home-media-l"></div>
<div id="home-media-text">More for this game</div>

<div id="home-media-r"></div>
</div></a>[/code]
Ok, I think part of your problem is "|1763|" does not equal "1763".

Try this:
First find the record that has the latest timestamp, get the "album", strip out the 2 "|" 's
Then, have a second query to get all of the stuff from the album table

This might be a little more bulky, but it will work for you.
Here just try this...  this is how I do my joins..  it works great...  Might be a bit dramatic for you but guaranteed to work...


[code]
<?php
$data = $turbo->get_all("SELECT users.user_id AS user_id,
users.username AS username,
    users.class AS class,
users.fname AS fname,
users.lname AS lname,
users.email AS email,
users.reg_date AS reg_date,
users.tot_cnt AS tot_cnt,
users.var_stats AS var_stats,
count(ratings.status = 'complete') AS status,
ratings.user_id as r_user_id
FROM users LEFT JOIN ratings ON users.user_id=ratings.user_id
GROUP BY users.user_id ORDER BY users.user_id ASC"); ?>
[/code]

and I would agree with putting in like an a_id in your files table that has the album id that it corresponds to.. that way you dont need to do anything else except run your query..  it'll make your life easier trust me..
Chris, it's so that files can be linked to multiple albums, like |XXXX|XXXX| etc. However, we never had a use for it...

But now there are 15,000+ rows in the database, and the current site is relying on seeing the "|" symbol. So I've been forced into finding other ways of doing it >_>;;

I can't figure out how to strip the | symbols, however. I guess it would be something similar to this..?

[code]$bars = "\|";
$removebars = "";

$media = $turbo->get_all("SELECT files.file AS file,
files.album = ereg_replace($bars, $removebars, files.album) AS album,
files.timestamp AS timestamp,
albums.id AS id,
albums.directory AS directory,
FROM files LEFT JOIN albums ON files.album=albums.id
GROUP BY files.album ORDER BY files.timestamp DESC");

while($latestmedia = mysql_fetch_array($media))
{
echo ('...[/code]

I know I'm wrong however, as it brings me an error. I'm really new to PHP, so if someone could lend me a hand here, I'd appreciate it :-[
What was the error?

Try and see what this does:
$media = $turbo->get_all("SELECT files.file AS file,
files.album =".ereg_replace($bars, $removebars, files.album)." AS album,
files.timestamp AS timestamp,
albums.id AS id,
albums.directory AS directory,
FROM files LEFT JOIN albums ON files.album=albums.id
GROUP BY files.album ORDER BY files.timestamp DESC");

Prob won't work, but worth a try
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.