Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

mysql

  • This topic is locked This topic is locked
213 replies to this topic

#205 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 14 February 2011 - 11:51 PM

You would need to do this in another page, a quick example of selecting all the ids and dumping them in to table rows:
<?php
$res = mysql_query("SELECT id FROM test");
?>
<table>
<?php
while($row = mysql_fetch_assoc($res)) {
echo "<tr><td><img src='show.php?id=". $row['id'] ."'/></td></tr>";
}
?>
</table>

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#206 kwahedi

kwahedi

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 15 February 2011 - 08:34 PM

Dear alaxender:
I did fallow your instruction but it doesn't work
Can you show me one step a head in a details
thanks
  • 0

#207 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 15 February 2011 - 09:43 PM

You will still need to connect to the database before running any mysql_query commands, try adding this before my script:
$username = "root";
$password = "";
$host = "localhost";
$database = "test";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#208 kwahedi

kwahedi

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 15 February 2011 - 09:50 PM

yeh sir
i have already added this before my query
but the result is empty and nothing is return back without empty page
No error message but just empty page
  • 0

#209 fschutz

fschutz

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 16 February 2011 - 07:27 PM

Hi there,

this tutorial is really good, but in my computer it doesn't works.
Here's my code:


<?php
$id = $_GET["id"];
require_once('conexao.php');
conectar(); // make the connection with the database
echo $id;
$sql = mysql_query("SELECT foto FROM membros WHERE membro = ".$id);
$row = mysql_fetch_array($sql);
$image = $row["foto"];
if ($image !== false) {
header('Content-type: image/jpeg');
echo $image;
}
?>


When I call this script in the Google Chrome, doesn't happens (the page keeps blank). When I call this script in the IE 8, it creates an default "X" image (some blank image with a big red X in the middle)

I used the script of the first tutorial to upload the image.


<?
$tmpName = $_FILES['foto']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$imgData = fread($fp, filesize($tmpName));
$imgData = addslashes($imgData);
fclose($fp);
$sql = "INSERT INTO `membros` ( `nome` , `endereco` , `bairro` , `CEP` , `cidade` , `telefone` , `email` , `CPF` , `categoria` , `datanascimento` , `foto`, `senha` ) VALUES( '{$_POST['nome']}' , '{$_POST['endereco']}' , '{$_POST['bairro']}' , '{$_POST['CEP']}' , '{$_POST['cidade']}' , '{$_POST['telefone']}' , '{$_POST['email']}' , '{$_POST['CPF']}' , '{$_POST['categoria']}' , '{$dtnasc}' , '{$imgData}', '{$senha_crypt}') ";
mysql_query($sql) or die(mysql_error());
?>


Why can I do?? :mad::crying::crying:
I've seen that many people had successfully... what is happening? The type of the image is 'image/jpeg' when I upload the file, and the type of the field is BLOB.

Regards,
Fernando.:w00t:
  • 0

#210 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 16 February 2011 - 09:01 PM

Try removing the header() call and turn on full error reporting at the beginning of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
There will likely be an error before the image data is echoed, and in turn that error is generated with the image invalidating it.
  • -1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#211 kwahedi

kwahedi

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 16 February 2011 - 10:15 PM

Dear you can use this is code which i tried in my own computer and it is working properly

amusing this is your uploading page from where u can select a file for uploading.

add.php
--------
<html>
<form enctype="multipart/form-data" action="chadd.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
<html>
============================
This page is the controller by which you can add your image to your database table
just edit the database name and make query as want

then save it
i am sure that it will work

chadd.php
----------

<?php

// Create MySQL login values and 
// set them to your login information.
$username = "root";
$password = "";
$host = "localhost";
$database = "your_database_name";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);  



// Make sure the user actually 
// selected and uploaded a file image is the field name in the table 
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      

      // Create the query and insert
      // into our database. pic is the table name
      $query = "INSERT INTO pic";
      $query .= "(image) VALUES ('$data')";
      $results = mysql_query($query, $link);
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
}
else {
   print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>


kwahedi
:):):):):)

Edited by Roger, 14 April 2011 - 05:40 PM.
added code tags

  • 0

#212 kwahedi

kwahedi

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 21 February 2011 - 12:30 AM

Dear fschutz!

Dont print the $id befor the query

make your file the same as i am showing to u

<?php
 
    require_once('conexao.php'); 
    conectar(); // make the connection with the database
    $id = $_GET["id"];
  $sql = mysql_query("SELECT foto FROM membros WHERE membro = ".$id);
  $row = mysql_fetch_array($sql); 
    $image = $row["foto"];
    if ($image !== false) {
         header('Content-type: image/jpeg');
     echo $image;
    }
?>

Edited by Roger, 14 April 2011 - 05:40 PM.
added [CODE][/CODE] tags

  • 0

#213 AARABI MADHAVAN

AARABI MADHAVAN

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 14 April 2011 - 06:07 AM

i have used ur above code for retrieving image from database.
but the output is not an image but the same url of the current php file. but when i right click on that output ( which is the same url ) i have options such as copy image , copy image location , save image ..ect.... what is the problem.??? why am i not able to view the image???
this is the code i have used..
<html>
<head>
</head>
<body>
<?php
$mysql =mysql_connect("localhost","root","") or die("could not connect to mysql");
mysql_select_db("rubikl") or die(mysql_error());
$id = $_GET['id'];
echo "$id";
if(empty($id))
{
     echo "Please select your image!";
}
else
{
$query = mysql_query("SELECT image FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
     echo $content;
}
?>
</body>
</html>
<?php
	mysql_close();
?>

Edited by Roger, 14 April 2011 - 05:41 PM.
added [CODE][/CODE] tags

  • 0

#214 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 14 April 2011 - 06:57 PM

AARABI MADHAVAN: Your issue is that you can only echo image related data, not any HTML or whitespace.
<?php
$mysql = mysql_connect("localhost","root","") or die("could not connect to mysql");
mysql_select_db("rubikl") or die(mysql_error());

//for safety, escape
$id = mysql_real_escape_string($_GET['id']);
if(empty($id))
{
//cannot form an image
exit;
}
else
{
$query = mysql_query("SELECT image FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
}

mysql_close();
?>
You can test this script by visiting script.php?id=1 in example, it will make an image not an HTML page.

------------

If anybody has any further questions, feel free register and create a new thread:
http://forum.codecall.net/php-forum/

It is not always best to post pages and pages of questions on a tutorial, it is better left to the appropriate forums.

Edited by Alexander, 07 May 2011 - 01:52 AM.

  • -1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.