Jump to content

Dynamic Image won't display from MySQL, shows up as random symbols. HELP!


czs

Recommended Posts

Hi all,

 

I'm pretty new with php and mysql.  I'm using dreamweaver to create a site.  I'm trying to get the site to lead an image stored in a mysql database but it just comes up as a bunch or random sybmols.  Here is my code.  Thank you very much for your help it's much appreciated!

 

I think this code fetches the image.


<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_RsPic = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_RsPic = $_SESSION['MM_Username'];
}
mysql_select_db($database_connPFD, $connPFD);
$query_RsPic = sprintf("SELECT * FROM tblprofile WHERE email = %s", GetSQLValueString($colname_RsPic, "text"));
$RsPic = mysql_query($query_RsPic, $connPFD) or die(mysql_error());
$row_RsPic = mysql_fetch_assoc($RsPic);
$totalRows_RsPic = mysql_num_rows($RsPic);
?>

 

and this code displays it:

 


<div id="mainContent">
    <h1> Guys Match Page</h1>
    <p><img src="<?php echo $row_RsPic['picture']; ?>" /></p>
    <!-- end #mainContent -->
  </div>

 

Thanks!

 

Link to comment
Share on other sites

your HTML code <img src=""> tag is just supposed to tell the browser where to go find the image.

 

if the image is saved somewhere on your website and the mysql value you're populating in there is the path to that image, then you should be all set.

 

if you've stored the image in your table as a blob, then you need to turn that blob of code back into an image.

 

something like a page called "image.php"

that you would call like <img src="image.php?imageID=1219382">

 

then the image.php would look like:

<?php

$id = $_GET['imageID'];
$getImage = mysql_query("SELECT column_name FROM table WHERE imageID='$id'");
$imageData = mysql_result($getImage,0,0);

header('Content-type: image/jpeg'); // tell browser what type of file this is.
header('Content-Disposition: inline; filename="some_image_name.jpg"'); // send the file name to the browser.

echo $imageData; //output the blob of data that the browser now knows is an image
?>

 

Link to comment
Share on other sites

Thank you so much for your help but I don't quite get what you mean.  I have to create a whole different page to store the code that will turn the blob into the image, and then reference that page to get the image to display?

 

This is my situation.  A user logs in, session is stored.  Then on the next page I have a recordset that grabs the users picture from the database based on the session variable.  Then I want it to display.  Basically it's a user logging in and having a profile with their picture there. 

 

Either way, i created an extra page and tried my best to get it to work, here is what I got.

 

in the new page that I made per your request called imagepage.php this is what I have in there:  For where the picture is stored, The table is called profile and the field is called picture. 

 


<?php
  session_start();
  echo $_SESSION['MM_Username']
  ?>
<?php require_once('Connections/connPFD.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_rspicture = "-1";
if (isset($_SESSION['MM_username'])) {
  $colname_rspicture = $_SESSION['MM_username'];
}
mysql_select_db($database_connPFD, $connPFD);
$query_rspicture = sprintf("SELECT * FROM tblprofile WHERE email = %s", GetSQLValueString($colname_rspicture, "text"));
$rspicture = mysql_query($query_rspicture, $connPFD) or die(mysql_error());
$row_rspicture = mysql_fetch_assoc($rspicture);
$totalRows_rspicture = mysql_num_rows($rspicture);
?><!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>Untitled Document</title>
</head>

<body>




<p>
  <?php

$id = $_GET['imageID'];
$getImage = mysql_query("SELECT picture FROM profile WHERE imageID='$id'");
$imageData = mysql_result($getImage,0,0);

header('Content-type: image/jpeg'); // tell browser what type of file this is.
header('Content-Disposition: inline; filename="picture.jpg"'); // send the file name to the browser.

echo $imageData; //output the blob of data that the browser now knows is an image
?>
  
  
</p>
</body>
</html>
<?php
mysql_free_result($rspicture);
?>

 

Then this is what I changed the image source to on the page where I actually want the image to be displayed:

 

<img src="imagepage.php?imageID=$id"

 

Thanks again for your help

 

 

your HTML code <img src=""> tag is just supposed to tell the browser where to go find the image.

 

if the image is saved somewhere on your website and the mysql value you're populating in there is the path to that image, then you should be all set.

 

if you've stored the image in your table as a blob, then you need to turn that blob of code back into an image.

 

something like a page called "image.php"

that you would call like <img src="image.php?imageID=1219382">

 

then the image.php would look like:

<?php

$id = $_GET['imageID'];
$getImage = mysql_query("SELECT column_name FROM table WHERE imageID='$id'");
$imageData = mysql_result($getImage,0,0);

header('Content-type: image/jpeg'); // tell browser what type of file this is.
header('Content-Disposition: inline; filename="some_image_name.jpg"'); // send the file name to the browser.

echo $imageData; //output the blob of data that the browser now knows is an image
?>

 

 

 

Link to comment
Share on other sites

Well since you have it all on the same page then you need to feed the url with the GET request to it already, otherwise the GET is empty, whereas if you feed it in from another page like micah1701 said your calling the source from that page with the variable from this page....

Link to comment
Share on other sites

<?php
$id = $_GET['imageID'];
?>

 

is at the top of imagepage.php.....the GET is not populated yet so $id has no value....whereas if you have imagepage2.php inside of your <img src> tag you are requesting a new page with the url already inside of it....check this....

 

 

imagepage.php
<?php
  session_start();
  echo $_SESSION['MM_Username']
  ?>
<?php require_once('Connections/connPFD.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_rspicture = "-1";
if (isset($_SESSION['MM_username'])) {
  $colname_rspicture = $_SESSION['MM_username'];
}
mysql_select_db($database_connPFD, $connPFD);
$query_rspicture = sprintf("SELECT * FROM tblprofile WHERE email = %s", GetSQLValueString($colname_rspicture, "text"));
$rspicture = mysql_query($query_rspicture, $connPFD) or die(mysql_error());
$row_rspicture = mysql_fetch_assoc($rspicture);
$totalRows_rspicture = mysql_num_rows($rspicture);
?><!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>Untitled Document</title>
</head>

<body>




<p>
  
<img src="imagepage2.php?imageID=$row_rspicture(???????)">
  
</p>
</body>
</html>
<?php
mysql_free_result($rspicture);
?>

imagepage2.php

<?php

$id = $_GET['imageID'];
$getImage = mysql_query("SELECT picture FROM profile WHERE imageID='$id'");
$imageData = mysql_result($getImage,0,0);

header('Content-type: image/jpeg'); // tell browser what type of file this is.
header('Content-Disposition: inline; filename="picture.jpg"'); // send the file name to the browser.

echo $imageData; //output the blob of data that the browser now knows is an image
?>

Link to comment
Share on other sites

Most would suggest that you just store the image in a folder on the server... then store the path to the image in the db and access the image by echoing the path to the img tag.

 

for instance

 

<?php

// You would get this from the db instead of setting it, this is just for example sake
$imagepath = "/images/uploads/pic01.jpg";

echo "<img src=\"$imagepath\">";

?>

 

Storing the image in the db really just adds processing steps that are not really needed.

 

Hope that helps,

 

Matt

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.