0

I am trying display images on webpage, where image path stored in database and images is stored in server.But i am not able to display those images using following codes, so pls somebody help me with this issue,..

<form method="post"  enctype="multipart/form-data" action="file_upload.php">
<table>

<?php

$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';


$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");

$path1 = mysql_query("select * from '$tbl_name' where id='1'");
$path2 = mysql_query("select * from '$tbl_name' where id='2'");
$path3 = mysql_query("select * from '$tbl_name' where id='3'");

echo '<tr><td><img src="$path1"></td>' ;
echo '<td><img src="$path2"></td>' ;
echo '<td><img src="$path3"></td></tr>' ;

?>

</table>
</form>
1
  • 1
    you need to loop over your result set $path1 with a function like mysql_fetch_assoc, and then select the column $path1['image_path'] Commented Oct 15, 2014 at 6:22

3 Answers 3

3

A couple of things before we begin:

  • I recommend using mysqli, and will do so in my examples below (mysql is deprecated)
  • I'll use a cycle to iterate through results, instead of querying each element individually.

PHP code

$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';


$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!$conn)
{
    die('Could not connect: ' . mysqli_connect_error());
}

$result = mysqli_query($con, "SELECT * FROM `$tbl_name`");

while ($row = mysqli_fetch_array($result))
{
    echo '<tr><td><img src="'.$row['image'].'"></td>' ;
}

Note how I first "fetched" the results from the query. The query first returns a mysqli object, one that contains all the results the query returned. These have to be extracted; the method I present is widely used in examples elsewhere as well.

Also note how the backtick character was used instead of single quotes when referring to the table.

Sign up to request clarification or add additional context in comments.

2 Comments

thanx for code i didnt understand $img_base = 'path/to/images';
I was assuming that your folder setup was this: /path/to/images/image.jpg. I edited my code a little bit. By first assigning 'path/to/images/' to $img_base and then assigning the actual image file to $img_item we can concatenate them together to get the url to your image.
0

After execute the query we will get the result set cursor. We need to iterate it to get all the rows . Try the below code it should work.

$result = mysql_query("SELECT * FROM  '$tbl_name' WHERE id IN ( 1, 2, 3 ) ");

if (!$result) {
// show your respective error messages
}else{
while ($row = mysql_fetch_assoc($result)) {
    echo '<tr><td><img src="'.$row['database_column_name'].'"></td>' ;
}
}

Comments

0

mysql_query(); has 2 arguments.

argument1: the connection.
argument2: the query.

this is what i will do if i was you:

$sql = "select * from `$tbl_name` where `id` between 1 and 3";
$path = mysql_query($conn, $sql);
while($row = mysqli_fetch_array($patht)) {
echo '<tr><td><img src="' . $row['name of colum'] . '"></td></tr>' ;
}
mysql_close($con);

sorry for my bad english. i'm Dutch.

1 Comment

thanx for the code what is "word" in echo $row['word']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.