Hi
I am trying to make a bike shop for my project and i need to pull out a product details with image from sql databse. I have made a table with products and i have stored the images url but i have no idea how to pull it out on to php. All i get is the url in text. Can any one help?

Recommended Answers

All 23 Replies

If you can get the URL in text all you have to do is put it into an HTML <img> tag:

<img src="<?php echo $row['image']; ?>" alt="your alt text" />
Member Avatar for diafol

I fyou showed your table and any code that you've tried?

So my table lokks like this.

1   id  int(11)             No  None    AUTO_INCREMENT  
    2   description     varchar(100)    utf8_general_ci         
    3   picurl  varchar(100)    utf8_general_ci         
    4   price   decimal(10,0)           
    5   stockcount  int(11)             
    6   weight  int(11)             
    7   category    int(11)             
    8   detail  text    utf8_general_ci     

And the php code looks like this:

$result = mysql_query("SELECT * FROM products");
echo "<table border='1'>
<tr>
<th>Bike ID</th>
<th>Bike Name ID</th>
<th>Picture</th>
<th>Price</th>
<th>How much we got</th>
<th>Weight</th>
<th>Type ID</th>
<th>Details</th>
</tr>";
while($row = mysql_fetch_array($result))

  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "<td>" . $row['picurl'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['stockcount'] . "</td>";
  echo "<td>" . $row['weight'] . "</td>";
  echo "<td>" . $row['category'] . "</td>";
  echo "<td>" . $row['detail'] . "</td>";

  echo "</tr>";
  }
echo "</table>";



mysql_close($con);

I have tryde to use <img src="<?php echo $row['picurl']; ?>" alt="your alt text" /> but it didn't work for some reson :/

Member Avatar for diafol

I have tryde to use <img src="<?php echo $row['picurl']; ?>" alt="your alt text" /> but it didn't work for some reson :/

SO what do you get?

I was told it's not a good practice to store images in a database because they take lots of space in the database?

What is the best solution to that?

I know this question doesn't fit in this thread but it might be primarily helpful to the thread owner since he's using images as well as other people who are interested in knowing that area of knowledge.

thanks.

2rotten69:- A database call is always costlier than a file system.When you store image in file system and just store location of image in DB as whenever you are storing file it will be encoded in binary format(BLOB ->Binary Large Object) and when you retrieve it ,it will be again decoded.

I have tryde to use <img src="<?php echo $row['picurl']; ?>" alt="your alt text" /> but it didn't work for some reson :/

Just check if it is a valid location by changing your URL to the absolute path of location specified in DB.

hai kamil.metkowski,

i used the url what you shown in the above post as shown bellow

<html>
<head>
<title>this is my first html page </title>
</head>
<body>
<img src="http://metkowski.net/KlempBikes/images/bmx1.jpg" alt="no img found"/>
</body>
</html>

its worked for me but you are saying that gives an error

do you have internet connection to your pc/laptop while running the page?(to display the resouce which is availble in the internet)

i feel this is also one reason for not displaying the image also

check it one more time

any comments are appreciated

Yes i know, but the url is stored in the database because there are many images that i will need to take out. How can i do this?

could you just post the picurl column values of your table here?

just i need that column data only

let me check once if you don't have any problem

You will need to store the image link into the database, so your database should be created with a SQL command such as;

CREATE TABLE `items` (
  `item_id` int(12) NOT NULL auto_increment,
  `product_category` varchar(20) NOT NULL,
  `product` varchar(30) NOT NULL,
  `manufacturer` varchar(50) NOT NULL,
  `product_color` varchar(25) NOT NULL,
  `product_price` int(12) NOT NULL,
  `product_quantity` int(12) NOT NULL,
  `product_image` longblob NOT NULL,
  `product_image_name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`item_id`)
) ;

Then, the next thing you want to do is to add a folder within your application folder and name it "uploads".
Then use the code below to upload the items along with their images to the database;

    $target_path = "uploads/"; // This is the folder where the images will be saved
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    $insert_items = "insert into items (item_id,product_category,product,manufacturer,product_color,product_price,product_quantity,product_image_name,product_image,description) values ('','$category','$product_name','$manufacturer','$product_color','$product_price','$product_quantity','$_FILES[uploadedfile]','".$target_path."','$desc')";
    $result = mysql_query($insert_items) or die("Unable to log because: ".mysql_error());
    $information="The file ". $target_path . " has been uploaded and submitted into the database";
    } else{
        $information="There was an error uploading the Photo, please try again!";
    }

That done, then u can simply retrieve the items using the usual mysl query, bt use the code below to display the image;

  <?php  echo "<img src=$rows[product_image] width=80 height=60>";  ?>

This is simply something I did some time ago, so; It may not be exact with your code, but I hope it will give you a clear picture.

I was told that storing images in a database is not a good idea its much better to store the url and take it from there?

Could you re-phrase the question ...

In my databse i have the url of an image and on my website i want to see the image not the url.

Then use the code below to upload the items along with their images to the database;

May sound like a dumb question here, but...
1. Where do i put this code?
2. How do i execute it?
i get that the code will enter all images in the upload folder, into the database, after i execute the code... But i dont get how it works?
3. Are the images still in the folder after being entered in the database?
4. does it constantly execute, waiting for another image to be copied into the upload folder?
can you explain a little more about this please?

@kamil.metkowski Look, change the data type of the "product_image" to text or varchar whichever you like. It will work the way you've got as <img src="<?php echo $row['image']; ?>" alt="your alt text" /> I don't think you'd have to do encode it to BLOB on the server then decode it on the client side.

There is no error in this line.Have you checked it is returning correct value for $row['image'].

<img src="<?php echo $row['image']; ?>" alt="your alt text" />

<?php
$var = "http://metkowski.net/KlempBikes/images/road1.jpg";
?>
<footer> <img src="<?php echo $var; ?>" alt="your alt text" />
</footer>

Because i have just used a variable instead of $row['image'] and it is giving correct data.
It is dislaying image.

Hi Thank you for your help. But it seems i don't quite get it. This is my php code:

$result = mysql_query("SELECT * FROM products
            where category = 1");

while ($row = mysql_fetch_array($result))
    {

    echo 'Product ID: '.$row['proid'];
    echo '<br/> Product Name: '.$row['description'];
    echo '<br/> Image: '.$row['picurl'];
    echo '<br/> Price: '.$row['price'];
      echo '<br/> Weight: '.$row['weight'];
      echo '<br/> Category: '.$row['category'];
      echo '<br/> Details: '.$row['detail'];
    echo '<br/><br/>';
    }

   mysql_close($con);
   <img src="<?php echo $row['image']; ?>" alt="your alt text" />

Is it the wrong place where i placed it? Can you please help?

Ow i see know i need to call it outside the php. Thanks a lot you have been really helpful.

I think you took $row['image'] from the code I provided in my original response. Should it not be $row['picurl'] instead? If you want the image to appear where you wrote echo ' <br /> Image: above, replace line 9 with the following code:

echo '<br/> Image: <img src="'.$row['picurl'].'" />';

Aside from that, if you've figured everything out, please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.