Unix Technical Forum

returning unique value

This is a discussion on returning unique value within the MySQL General forum forums, part of the MySQL category; --> I have two tables galleries which contains the number and name of the photo galleries and 'thumnails' the images ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:07 AM
Ross Hulford
 
Posts: n/a
Default returning unique value



I have two tables galleries which contains the number and name of the photo galleries and 'thumnails' the images that are conenected to the galleries.

I am trying to create a 'pick a gallery' screen where it selects all the galleries and then output the first thumbnail image associated with that gallery. The probroblem is only returning one unique image.





--
-- Table structure for table `galleries`
--

CREATE TABLE `galleries` (
`id` int(11) NOT NULL auto_increment,
`display` tinyint(4) NOT NULL default '0',
`galleryorder` int(11) NOT NULL default '0',
`title` mediumtext NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Dumping data for table `galleries`
--

INSERT INTO `galleries` VALUES (7, 1, 1, 'my gallery1', 'my gallery1');
INSERT INTO `galleries` VALUES (8, 0, 1, 'gallery2', 'my gallery2');

-- --------------------------------------------------------

--
-- Table structure for table `thumbnails`
--

CREATE TABLE `thumbnails` (
`id` int(4) NOT NULL auto_increment,
`gallery` int(4) NOT NULL default '0',
`display` tinyint(4) NOT NULL default '0',
`photoorder` int(4) NOT NULL default '0',
`caption` varchar(80) NOT NULL default '',
`description` varchar(200) default NULL,
`bin_data` longblob,
`filename` varchar(50) default NULL,
`filesize` varchar(50) default NULL,
`filetype` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=513 DEFAULT CHARSET=latin1 AUTO_INCREMENT=513 ;



This is what I have so far



<? $dbquery = "SELECT id FROM galleries";
$result = mysql_query($dbquery);
while($row=mysql_fetch_array($result))
{
echo $id=$row['id'];
$dbquery2 = "SELECT * FROM thumbnails where gallery=$id";
$result2 = mysql_query($dbquery2);
while($myimage=mysql_fetch_array($result2)){
echo $myimage['caption'];
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:07 AM
Dan Buettner
 
Posts: n/a
Default Re: returning unique value

Ross -

In your query you can add a "LIMIT" clause to get just one row in the
result, a la:
SELECT * FROM thumbnails where gallery=$id LIMIT 1

If you want the "first" image, say the one with lowest ID number, you
could do this:
SELECT * FROM thumbnails where gallery=$id ORDER BY id LIMIT 1

Dan


On 11/1/06, Ross Hulford <ross@aztechost.com> wrote:
>
>
> I have two tables galleries which contains the number and name of the photo galleries and 'thumnails' the images that are conenected to the galleries.
>
> I am trying to create a 'pick a gallery' screen where it selects all the galleries and then output the first thumbnail image associated with that gallery. The probroblem is only returning one unique image.
>
>
>
>
>
> --
> -- Table structure for table `galleries`
> --
>
> CREATE TABLE `galleries` (
> `id` int(11) NOT NULL auto_increment,
> `display` tinyint(4) NOT NULL default '0',
> `galleryorder` int(11) NOT NULL default '0',
> `title` mediumtext NOT NULL,
> `description` text NOT NULL,
> PRIMARY KEY (`id`),
> KEY `id` (`id`)
> ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
>
> --
> -- Dumping data for table `galleries`
> --
>
> INSERT INTO `galleries` VALUES (7, 1, 1, 'my gallery1', 'my gallery1');
> INSERT INTO `galleries` VALUES (8, 0, 1, 'gallery2', 'my gallery2');
>
> -- --------------------------------------------------------
>
> --
> -- Table structure for table `thumbnails`
> --
>
> CREATE TABLE `thumbnails` (
> `id` int(4) NOT NULL auto_increment,
> `gallery` int(4) NOT NULL default '0',
> `display` tinyint(4) NOT NULL default '0',
> `photoorder` int(4) NOT NULL default '0',
> `caption` varchar(80) NOT NULL default '',
> `description` varchar(200) default NULL,
> `bin_data` longblob,
> `filename` varchar(50) default NULL,
> `filesize` varchar(50) default NULL,
> `filetype` varchar(50) default NULL,
> PRIMARY KEY (`id`)
> ) ENGINE=MyISAM AUTO_INCREMENT=513 DEFAULT CHARSET=latin1 AUTO_INCREMENT=513 ;
>
>
>
> This is what I have so far
>
>
>
> <? $dbquery = "SELECT id FROM galleries";
> $result = mysql_query($dbquery);
> while($row=mysql_fetch_array($result))
> {
> echo $id=$row['id'];
> $dbquery2 = "SELECT * FROM thumbnails where gallery=$id";
> $result2 = mysql_query($dbquery2);
> while($myimage=mysql_fetch_array($result2)){
> echo $myimage['caption'];
> }
> }
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:03 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com