Reputation: 33
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>
Upvotes: 0
Views: 4335
Reputation: 31924
A couple of things before we begin:
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.
Upvotes: 3
Reputation: 23
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.
Upvotes: 0
Reputation: 354
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>' ;
}
}
Upvotes: 0