/    Sign up×
Community /Pin to ProfileBookmark

Two Databases Working together

I am trying to have two databases work together, however, I am having problems figuring it out.

DATABASE 1: FIELDS = id, title, description, image_name_id

DATABASE 2: FIELDS = id, image_name

I already wrote a script that uploads an image and writes the name of the image to “image_name” in database 2.
When I want to output that image, I just use the link where the image was stored in my ftp. E.g. http://www.mysite.com/upload/<? echo $onecontent->image_name ?>

However, I would like to do something different.
I would like to have DATABASE 1 used to store articles. Sometimes, the articles would have images associated with them. The same image may be used often by any given number of articles, hence the need for a database for the image. However, sometimes an image doesn’t exist.
I wrote a script already that allows me to add articles along with a selection of images that exist. That was easy. The hard part is the actual content page.

IF NOT IMAGE EXIST, then don’t display, however, if an image exist, then display the image.

[code=php]<? if($onecontent->image_name_id!=””) { ?>

<?php
include(“../class/config.php”);
$id=$onefea->image_name;
$start=$_REQUEST[‘start’];
$WHERE = “WHERE `id` = ” . $onefea->image_name;

$sqlfea = “SELECT * FROM `DATABASE2` $WHERE”;
$linkfea = mysql_query($sqlfea,$db);

while ($onefea = mysql_fetch_object($linkfea)) {
echo “<img border=0 src=’images/theme_images/”.$onecontent->image_name.”‘ />”; ?>
<? } ?> [/code]

I am so confused now and my code is totally wrong. Can someone send me in the right direction so that I can include the image name from DATABASE 2, into my script with output information for DATABASE 1.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@flannAug 20.2007 — lets say you have 2 DBs (database1 & database2)

database1 has a table called cheeses

cheeses has 2 fields cheeseID and cheese

database2 has a table called milk

milk has 2 fields called milkID and cheeseID

lets select all these fields.

SELECT c.cheeseID, c.cheese, m.milkID

FROM database1.cheeses as c

INNER JOIN database2.milk ON c.cheeseID = m.cheeseID

WHERE c.cheese = 'Cheddar'

I know my databases and tables make absolutely no sense, but it would work. What is happening in the FROM is I'm appending the database name to the beginning of the table name and the giving it an alias of 'c' because it will be a whole lot easier for me to work with. I'm doing roughly the same thing in the JOIN.

make sense?
Copy linkTweet thisAlerts:
@gc40authorAug 20.2007 — lets say you have 2 DBs (database1 & database2)

database1 has a table called cheeses

cheeses has 2 fields cheeseID and cheese

database2 has a table called milk

milk has 2 fields called milkID and cheeseID

lets select all these fields.

SELECT c.cheeseID, c.cheese, m.milkID

FROM database1.cheeses as c

INNER JOIN database2.milk ON c.cheeseID = m.cheeseID

WHERE c.cheese = 'Cheddar'

I know my databases and tables make absolutely no sense, but it would work. What is happening in the FROM is I'm appending the database name to the beginning of the table name and the giving it an alias of 'c' because it will be a whole lot easier for me to work with. I'm doing roughly the same thing in the JOIN.

make sense?[/QUOTE]


I think I messed up my request today.

I TOTALLY MESSED IT UP. It is NOT TWO DATABASES!! It is two tables in one database. OMG!! AHHHH!!!
Copy linkTweet thisAlerts:
@flannAug 20.2007 — it's pretty much the same thing, just take off the database names or you could even leave them on if you wish.
×

Success!

Help @gc40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...