Need help with displaying picture from mysql database. Can anyone provide a basic php script to retrieve picture from database for display in html page. I have database field: avatar_url whichs save filename of picture in database and a folder: images/avatars where the picture get saved saved. This is for jpeg, gif and png files. I have a profile page for users where I want to show their picture next to their related information in a table. The problem is that the picture does show on the page. What am I doing wrong (please see code below)
Appreciate any help.
Thanks

<?php
include 'dbc.php';
page_protect();
$member = @$_GET['member'];
/******************** ADD ME TO SHOW USERS AVATAR ****************************/
$img_url_query = mysql_query("select avatar_url from users where id = '$_SESSION[user_id]'");
while($img_url_settings = mysql_fetch_array($img_url_query)) {
$img_url = $img_url_setting['avatar_url'];
}
/*****************************************************************************/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>profile search</title>
<link rel="stylesheet" href="css/default.css" type="text/css" media="screen"/>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td width="160" valign="top">
<?php
if(isset($_SESSION['user_id'])) {?>
<div class="myaccount">
<p><strong>My Account</strong></p>
<a href="myaccount.php">My Account</a><br>
  <a href="mysettings.php">Settings</a><br>
    <a href="profile.php">User Profile </a><br>
    <a href="logout.php">Logout </a>
<p>You can add more links here for users</p></div>

<?php }?>

<td width="732" valign="top"><p></p>
<h3 class="titlehdr">Profile of <?php echo $member;?></h3>
<table width="80%" border="0" align="center" cellpadding="10" cellspacing="5" bgcolor="#e5ecf9">
<tr>
<td><form name="form1" method="get" action="profile.php">
User Name:<input name="member" type="text" />
<input type="submit" value="Search" />
</form></td>
</tr>
</table>

<?php
if(isset($_GET['msg'])) {
echo"<div class=\"error\">$_GET[msg]</div>";
}
$query ="SELECT * FROM users WHERE user_name = '$member'";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
if($num ==0){
echo"No results to display, please search again.";
}else{
$i=0;
while($i<1){
$acc_name = mysql_result($result,$i,"acc_name");
$user_name = mysql_result($result,$i,"user_name");
$user_email = mysql_result($result,$i,"user_email");
$website = mysql_result($result,$i,"website");
$acc_profile = mysql_result($result,$i,"acc_profile");
$acc_location = mysql_result($result,$i,"acc_location");
$acc_directions = mysql_result($result,$i,"acc_directions");
$date_registered = mysql_result($result,$i,"date_registered"); 
$avatar_url = mysql_result($result,$i,"avatar_url");
echo "<table border RULES=NONE FRAME=BOX WIDTH=\"100%\">
<tr>
<td>User Name:</td><td>$user_name</td>
</tr>
<tr>
<td width = \"50%\">Name:</td><td>$acc_name</td>
</tr>
<tr>
<td>Email Address:</td><td>$user_email</td>
</tr>
<tr>
<td>Website:</td><td>$website</td>
</tr>
<tr>
<td>Acc Profile:</td><td>$acc_profile</td>
</tr>
<tr>
<td>Acc Location:</td><td>$acc_location</td>
</tr>
<tr>
<td>Acc Directions:</td><td>$acc_directions</td>
</tr>
<tr>
<td>Date_registered:</td><td>$date_registered</td>
</tr>
<tr>
<td>Photo:</td><td><img src ='images/avatars/''.$avatar_url.''width='240' 'height='180'> <br /></td>
</tr>

</table><br><br>";
$i++;
}
}
?>
</td>
<td width="196" valign="top"> </td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
</body>
</html>

Recommended Answers

All 8 Replies

Need help with displaying picture from mysql database. Can anyone provide a basic php script to retrieve picture from database for display in html page. I have database field: avatar_url whichs save filename of picture in database and a folder: images/avatars where the picture get saved saved. This is for jpeg, gif and png files. I have a profile page for users where I want to show their picture next to their related information in a table. The problem is that the picture does show on the page. What am I doing wrong (please see code below)
Appreciate any help.
Thanks

<?php
include 'dbc.php';
page_protect();
$member = @$_GET['member'];
/******************** ADD ME TO SHOW USERS AVATAR ****************************/
$img_url_query = mysql_query("select avatar_url from users where id = '$_SESSION[user_id]'");
while($img_url_settings = mysql_fetch_array($img_url_query)) {
$img_url = $img_url_setting['avatar_url'];
}
/*****************************************************************************/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>profile search</title>
<link rel="stylesheet" href="css/default.css" type="text/css" media="screen"/>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td width="160" valign="top">
<?php
if(isset($_SESSION['user_id'])) {?>
<div class="myaccount">
<p><strong>My Account</strong></p>
<a href="myaccount.php">My Account</a><br>
  <a href="mysettings.php">Settings</a><br>
    <a href="profile.php">User Profile </a><br>
    <a href="logout.php">Logout </a>
<p>You can add more links here for users</p></div>

<?php }?>

<td width="732" valign="top"><p></p>
<h3 class="titlehdr">Profile of <?php echo $member;?></h3>
<table width="80%" border="0" align="center" cellpadding="10" cellspacing="5" bgcolor="#e5ecf9">
<tr>
<td><form name="form1" method="get" action="profile.php">
User Name:<input name="member" type="text" />
<input type="submit" value="Search" />
</form></td>
</tr>
</table>

<?php
if(isset($_GET['msg'])) {
echo"<div class=\"error\">$_GET[msg]</div>";
}
$query ="SELECT * FROM users WHERE user_name = '$member'";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
if($num ==0){
echo"No results to display, please search again.";
}else{
$i=0;
while($i<1){
$acc_name = mysql_result($result,$i,"acc_name");
$user_name = mysql_result($result,$i,"user_name");
$user_email = mysql_result($result,$i,"user_email");
$website = mysql_result($result,$i,"website");
$acc_profile = mysql_result($result,$i,"acc_profile");
$acc_location = mysql_result($result,$i,"acc_location");
$acc_directions = mysql_result($result,$i,"acc_directions");
$date_registered = mysql_result($result,$i,"date_registered"); 
$avatar_url = mysql_result($result,$i,"avatar_url");
echo "<table border RULES=NONE FRAME=BOX WIDTH=\"100%\">
<tr>
<td>User Name:</td><td>$user_name</td>
</tr>
<tr>
<td width = \"50%\">Name:</td><td>$acc_name</td>
</tr>
<tr>
<td>Email Address:</td><td>$user_email</td>
</tr>
<tr>
<td>Website:</td><td>$website</td>
</tr>
<tr>
<td>Acc Profile:</td><td>$acc_profile</td>
</tr>
<tr>
<td>Acc Location:</td><td>$acc_location</td>
</tr>
<tr>
<td>Acc Directions:</td><td>$acc_directions</td>
</tr>
<tr>
<td>Date_registered:</td><td>$date_registered</td>
</tr>
<tr>
<td>Photo:</td><td><img src ='images/avatars/''.$avatar_url.''width='240' 'height='180'> <br /></td>
</tr>

</table><br><br>";
$i++;
}
}
?>
</td>
<td width="196" valign="top"> </td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
</body>
</html>

try this in your line 7

while($img_url_settings = mysql_fetch_object($img_url_query)) {

replace array with object.

i hope this helps, though i didn't try it myself.

good luck. :icon_wink:

Hi everyone.
I in my previous post I said the picture does show from the database. In fact it does not display on the page. Sorry for the confusion.
Thanks.

Hi everyone.
I in my previous post I said the picture does show from the database. In fact it does not display on the page. Sorry for the confusion.
Thanks.

Hi everyone.
I in my previous post I said the picture does show from the database. In fact it does not display on the page. Sorry for the confusion.
Thanks.

yeah, it's understood 'coz there will be no problem if it does showed in your page already.
:idea:

good practice is to echo your path on script and see if the path is correct!

line 98
your using double quotes so you don't need the ''. .''
change to <img src =\"images/avatars/$avatar_url\" width='240' 'height='180'>
or <img src ='images/avatars/{$avatar_url}' width='240' 'height='180'>

Metalix, thanks for your help. The picture gets displayed on page. Thanks to other forum members for their input.

You welcome
Remember to mark this thread as solved if its 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.