Jump to content

Problem of print the images from mysql database


mark103

Recommended Posts

Hi guys,

 

I have a little trouble right here. I have uploaded the images and stored in mysql database. When I get access to my website, the images doesn't print out which it displayed and looks like this:

 

dbimages.jpg

 

 

How I can print the images from mysql database when I uploaded the images?

 

Here's the current code:

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'dbusername');
    define('DB_PASSWORD', 'dbpassword');
    define('DB_DATABASE', 'dbname');
       
    $errmsg_arr = array();
    $errflag = false;

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
  die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {

die("Unable to select database");
    }

   function clean($var){

return mysql_real_escape_string(strip_tags($var));
    }
    $username = clean($_GET['user']);
    $pass = clean($_GET['pass']);


if($username == '' && $pass == ''){
   // both are empty
   $errmsg_arr[] = 'Username and password are missing. You must enter both or the other one.';
   $errflag = true;
}

    if($errflag) {
  $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  echo implode('<br />',$errmsg_arr);
   }
   else {

$insert = array();
if(isset($_GET['user'])) {
    $insert[] = 'username = \'' . clean($_GET['user']) .'\'';
}
if(isset($_GET['pass'])) {
    $insert[] = 'pass = \'' . clean($_GET['pass']) . '\'';
}



if (count($insert)>0) {
   $names = implode(',',$insert);


if($username) {
  $qrytable1="SELECT username, dbimages  FROM images_list WHERE username='$username'";
  $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());

while ($row = mysql_fetch_array($result1)) {
  echo "<p id='dbimages'>";
  echo $row['dbimages'] . "</p>";
  }
}
}
?>

 

 

Any help would be much appreicated.

 

thanks in advance

Probably best idea and what you need to do is create a php file for displaying the images, and in this file you get the image data from the db say for example show_image.php

 

show_image.php

// Get image data from db by id or username or whatever condition first and store it to variable

// Send correct header, with image MIME type
header("Content-type: image/jpeg");

// Output image data from database (this var holds the image data).
echo $imageDataFromDB;

 

Then in yhe page you want to show the images something like this

<img src="show_image.php?image_id=3">

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.