Hi,

I have in my database the location of some images I want to display, all formatted as:

../images/products/1.jpg
../images/products/1.jpg

I'm trying to diplay them in a webpage but get the following error:

x
Warning: substr() expects parameter 1 to be string, array given in C:\xampp\htdocs\mysite\index.php

x
Warning: substr() expects parameter 1 to be string, array given in C:\xampp\htdocs\mysite\index.php

The 'x's display fine, matching the number of row in the database, but the images do not.

Here is my code:

<?php
$query = "SELECT product_Picture from products LIMIT 0, 10";
$result = mysql_query($query) or die(mysql_error());
$x=0;
while($row = mysql_fetch_array($result))
{
	echo 'x';//these work
	$row[x] = substr($row, 3);//removes the leading .. from the stored URLs
	echo "<img src='".$row."' /><br />";
}
?>

Can anyone help me correct this problem?

Your help is most appreciated!

Thanks!

Recommended Answers

All 4 Replies

$image = substr($row['image'], 3);
echo "<img src='".$image."' /><br />";

That's assuming the image location is stored in a column called "image" in your database table.

In addition to that.. I'm pretty sure you need to insert \'s before your quotes in the <img> tag so the "s are printed and not seen as part of the php code.

@CodeEgg 's method may work allthough I'm not entirely convinced the method I prefer is using a document called "img.php" this document will need to process an ID or something that defines the row which contains the image you wish to display. You would do this via the GET method, this can be forced by linking img.php?id=IMAGE_ID .

Then you will use the get method to query the database, following this you will need to verify that there was a row returned. If there is a row then you need to set a header as shown below to tell the browser it is an image of the format you specified in the database.

After this all you need to do is echo out the contents of the blob field.

IF the if function returns false (or there wasn't an image by that ID) then you will need to again print the header for an image type allong with an error image. As stated below you MUST use an image as this document will be pointed to via an IMG tag so it will have to return an image of sorts. Text will not be shown and the img tag will just show a red cross to tell the user that there is no image.

In this document it will need to pull the image type column (or mime type etc.) and print it as a header:

<?php

$con = mysql_connect("server_ip" , "db_username", "db_password");
mysql_select_db("db_name" , $con );

$query = mysql_query("SELECT * FROM `images` WHERE `id` = '" . mysql_real_escape_string( $_GET['id'] ) . "'");

if( @mysql_num_rows( $query ) > 0 ) {
    $row = mysql_fetch_object( $query );
    header("Content-type: " . $row->mime);
    echo $row->blob;
} else {
    // This will be triggered if the image is not found, if this is the case TEXT CAN NOT BE USED AS THIS IS SUPPOSED TO BE AN IMAGE
    // If this is the case a good idea is to repeat the proccess above but retrieve an error image from the database that will say like "error image not found" or something allong those lines
}

?>

This is off of the top of my head but the theory should be correct if not I'm sure someone will correct it allong with an insult toward me knowing daniweb members.

So I hope this helps, the document above can be used but I'm not entirely sure it works it's just for demonstration but usually they work without major errors.

How can i open an image in new window when click on thumbnail. i am beginner in Php.

<?php 

require("connect.php");

$album =mysql_query("SELECT * FROM albums");

echo "<table width='22%'>";
while ($row = mysql_fetch_assoc($album))
{
	echo "
	<tr>
	   <td>
	      <img src='images/".$row['cover']."' width='100%' height='100%'>
	   </td>
	   <td>
	      <b>".$row['name']."</b><br>
	      ".$row['description']."
	   </td>
	</tr>
	

	";
}
echo "</table>";




?>

First off, you should start a new thread and not take over an existing thread. Secondly, that has nothing to do with PHP, it's basic HTML. Add this to a link...

target="_blank"

How can i open an image in new window when click on thumbnail. i am beginner in Php.

<?php 

require("connect.php");

$album =mysql_query("SELECT * FROM albums");

echo "<table width='22%'>";
while ($row = mysql_fetch_assoc($album))
{
	echo "
	<tr>
	   <td>
	      <img src='images/".$row['cover']."' width='100%' height='100%'>
	   </td>
	   <td>
	      <b>".$row['name']."</b><br>
	      ".$row['description']."
	   </td>
	</tr>
	

	";
}
echo "</table>";




?>
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.