简体   繁体   中英

Displaying an image where image path stored in Database using Id,

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>

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.

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.

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>' ;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM