Guidance needed with getting images from db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ulstulst
Forum Newbie
Posts: 2
Joined: Mon Nov 30, 2009 5:19 am

Guidance needed with getting images from db

Post by ulstulst »

Hello, I apologize beforehand, I have been up for two days straight so if i do not make any sense I bow and say sorry.

so my problem is getting pictures from the database

the code is as follows

Code: Select all

<head>
<title>Tooted: Haakeriistad</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
<style type="text/css">
<!--
img{width: 150px;}
}
-->
</style>
<
</head>
<body bgcolor="#999966" onload="initLightbox()">
<span class="pageName">
<?php
function connect(){
mysql_connect('localhost', 'root', '') or die(mysql_error());;
mysql_select_db('pold');
}
 
function toode(){
connect();
$sql = "SELECT * FROM traktor";
$result = mysql_query($sql);
 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo 'Name: '.$row['name'].'<br />';
echo 'Pilt 1: <img src="trakats/'.$row['pilt1'].'">';
echo 'Pilt 2: <img src="trakats/'.$row['pilt2'].'">';
echo 'Pilt 3: <img src="trakats/'.$row['pilt3'].'"><br />';
echo 'Kirjeldus: '.$row['kirjeldus'].'<br />';
echo 'Hind: '.$row['hind'].'EEK','<br />';
echo '<br />';
}
}
toode();
?>
</span>
</body>
</html>
 
and what comes out is
http://picasaweb.google.com/ulstulst/Un ... 7522752674

As you can see the second row of pictures is a little bit off
I want to get it working so that when I leave the DB fields empty
the empty image signs do not appear

if i left something out please ask

thank you very much

Martin

p.s.
the db is as follows

Code: Select all

 
-
-- Database: `pold`
--
 
-- --------------------------------------------------------
 
--
-- Table structure for table `traktor`
--
 
CREATE TABLE IF NOT EXISTS `traktor` (
  `id` varchar(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `pilt1` varchar(20) NOT NULL,
  `pilt2` varchar(20) NOT NULL,
  `pilt3` varchar(20) NOT NULL,
  `kirjeldus` varchar(40) NOT NULL,
  `hind` varchar(15) NOT NULL,
  UNIQUE KEY `pilt1` (`pilt1`),
  UNIQUE KEY `pilt2` (`pilt2`),
  UNIQUE KEY `pilt3` (`pilt3`),
  UNIQUE KEY `kirjeldus` (`kirjeldus`),
  UNIQUE KEY `hind` (`hind`),
  UNIQUE KEY `name_2` (`name`),
  KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
--
-- Dumping data for table `traktor`
--
 
INSERT INTO `traktor` (`id`, `name`, `pilt1`, `pilt2`, `pilt3`, `kirjeldus`, `hind`) VALUES
('', 'traktor', 'maal 132_sized.jpg', 'maal 133_sized.jpg', 'maal 134_sized.jpg', 'no on ju traktor', '5000'),
('', 'trakats', 'maal 146_sized.jpg', '', '', 'öäõü', '77000');
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Guidance needed with getting images from db

Post by AbraCadaver »

Code: Select all

if(!empty($row['pilt1'])) { echo 'Pilt 1: <img src="trakats/'.$row['pilt1'].'">'; }
if(!empty($row['pilt2'])) { echo 'Pilt 2: <img src="trakats/'.$row['pilt2'].'">'; }
if(!empty($row['pilt3'])) { echo 'Pilt 3: <img src="trakats/'.$row['pilt3'].'"><br />'; }
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ulstulst
Forum Newbie
Posts: 2
Joined: Mon Nov 30, 2009 5:19 am

Re: Guidance needed with getting images from db

Post by ulstulst »

thanks a bunch you are a lifesaver

this will be a gift for my grandpa who deals in used agriculture machinery

so you will have made him a happy man

cheers martin
Post Reply