Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Display pictures from database

Posted on 2013-12-18
7
192 Views
Last Modified: 2014-01-06
Hi!

Have this page:

<?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);

  require_once('RAY_EE_config.php');
  require_once('Connect_databse.php');
  $table6 = 'ImageTable';

  $sql = "SELECT * FROM {$table6}";
  $resultraptmp = sqlsrv_query($conn,$sql);

?>

<table>
  

  <?php

  $sql = "SELECT * FROM {$table6}";
  $rettmp = sqlsrv_query($conn,$sql);

  if( $rettmp === false )
  {
       echo "Error in executing statement 7.\n";
       die( print_r( sqlsrv_errors(), true));
  }
  
  $antbilder=0;
  while($row3 = sqlsrv_fetch_array($rettmp)){

     $antbilder= $antbilder+1;         
  }
  
  ?> 

  <?php
  if($antbilder>0)
  {
     
  ?>
    
    <tr>
  <?php 
    $sql = "SELECT * FROM {$table6}";
    $rettmp2 = sqlsrv_query($conn,$sql); 
    $i=0;
    $bilde[ ]=array();
   while($row3 = sqlsrv_fetch_array($rettmp2)){
       
       $bilde1 = "data:image/jpg;base64,"; 
       $bilde2 = $row3['TransImage'];
       $bilde[$i] = $bilde1 . $bilde2; 
       $i=$i+1;
    ?>


  <?php } ?>
 

 <?php
   for($i=0;$i=$antbilder;$i++)
   { ?>

        <td valign="top" class="rapport_image1">
           <img src='<?php echo $bilde[$i] ?>' hight="190" align="left">
        </td>
   <?php  }?>


  <?php
    if($antbilder>0)
  {?></tr><?php }
}  ?> 
   
</table>    

Open in new window


Its only looping ?
0
Comment
Question by:team2005
  • 4
  • 2
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39726614
Its only looping ?
Whether the answer is "yes" or "no" the design you have here is going to be troublesome for you.  The correct design is to have the image files in the server file system and the URLs of the image files in the data base (along with any other information you want to keep about the images - tags, colors, etc).

Then you can generate HTML <img> tags.

Note that the header() must come first and be complete before any browser output whatsoever, even including invisible whitespace.  The HTML <table> statement on line 14 makes it impossible to issue header().
0
 
LVL 2

Author Comment

by:team2005
ID: 39728520
Hi!

The customer dont want the images stored as files, and they want to stored
the pictures in a database.

Why cant i use my code ?
0
 
LVL 2

Author Comment

by:team2005
ID: 39728877
Hi!

Sorry, my mistace...

I just have made this testfile, and forget to put in Header...
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39729126
I'm going to recommend that you step back from this project and take some time to get a foundation in the fundamentals of HTML and PHP.  This is a technically incompetent approach to the issues on many levels.  There is no sin in ignorance, and all of us had to start learning at some point.  But it would not be good to deliver something like this and call it a professional work product -- that would damage your reputation forever.

This article will help you find your way to useful learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Here are some answers to the question, "Why cant i use my code ?"  There are a lot of things that prevent it from working and impede your progress toward a workable solution.

For better or worse, computer programming is an activity that requires precision and attention to even the minute details.  So when we see something like hight="190", when we should be seeing a CSS style sheet that references height, it shows that we are not on the right path.  We do not use attributes to add style to the <img> tag any more.  That made sense in 1996, but not today.

When we see die() in the PHP code, that is a very bad code smell that indicates a lack of understanding of error handling.

When we see something like this code snippet (copied verbatim from the post above) one is left to wonder what the author was thinking.  Why is the script executing the same query twice in succession?  It doesn't make any sense.  And even further down the code, we see the same query run for a third time.  The query should be run once to retrieve a results set.  The results set is reusable.  
  require_once('Connect_databse.php');
  $table6 = 'ImageTable';

  $sql = "SELECT * FROM {$table6}";
  $resultraptmp = sqlsrv_query($conn,$sql);

?>

<table>
  

  <?php

  $sql = "SELECT * FROM {$table6}";
  $rettmp = sqlsrv_query($conn,$sql);

Open in new window

The numerous and repeated flip-flops in and out of PHP virtually guarantee that the code will be difficult or impossible to debug.  A general guideline for the PHP programmer is to create all of the PHP variables needed, then use a template to write the HTML document, perhaps using a HEREDOC string.  The more echo statements you see and the more embedded HTML you see, the less likely that the script will be successful.

There are no comments in the code to explain the logic, so it's going to be difficult for anyone else to read the code and understand its intent.  Comments are really useful.

I hope you will take some time to actually learn this, rather than just copying some code and hoping it will work.  There are good learning resources available if you will just take the time and effort to use them.  Here are the technologies you will need to learn.  Your web projects will typically integrate some or all of these technologies.

Client side: HTML, CSS and JavaScript (usually jQuery)
Server side: PHP, SQL (usually MySQL, but in this case SQL Server).

Here is an article about Client/Server protocols that may help you understand the relationship between the technologies.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

If the client is paying you to put an image in the database, you will need to have a script that can create an image document from the information that is retrieved from the database.  This script will be the URL target for the HTML <img> tag, src= attribute.  The usual design would give you an image tag that looks like this:

<img src="path/to/script.php?name=myImage.png" />

In this design, script.php will find the name key in the $_GET request arguments.  It will use a data base query to look up the row that contains myImage.png.  It will recognize that the file suffix is png, so it will issue the header like this:

header('Content-type: image/png');

If the name had been something like myImage.jpg the header would look like this:

header('Content-type: image/jpeg');

After issuing the header, the script would decode the image information from the database and send it to the browser.  You would not want to send more than one image from this script; it would be used in each of the HTML image tags to produce one image per <img> tag.  By using different name= request variables, you would be able to have one script that could render multiple images.

I hope this helps you get started learning the techniques.  Best of luck with your project, ~Ray
0
 
LVL 2

Expert Comment

by:ITTallahassee
ID: 39729146
Ray's Comments above are spot on.  I would recommend visiting his links as well.
0
 
LVL 2

Author Comment

by:team2005
ID: 39729169
Hi!

I just take out the code from a bigger file (about 800 lines of code)

But see that i missed out header++

Here is the modified file with header..

<?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);

  require_once('RAY_EE_config.php');
  require_once('Connect_databse.php');
  $table6 = 'ImageTable';

  $sql = "SELECT * FROM {$table6}";
  $resultraptmp = sqlsrv_query($conn,$sql);

?>

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title>Agrippa report system 2</title>
<link href="css/redmond/jquery-ui-1.8.13.custom.css" rel="stylesheet" />
<script src='js/jquery.min.js' type='text/javascript'></script>

</head>
<body>

<table>
  

  <?php

  $sql = "SELECT * FROM {$table6}";
  $rettmp = sqlsrv_query($conn,$sql);

  if( $rettmp === false )
  {
       echo "Error in executing statement 7.\n";
       die( print_r( sqlsrv_errors(), true));
  }
  
  $antbilder=0;
  while($row3 = sqlsrv_fetch_array($rettmp)){

     $antbilder= $antbilder+1;         
  }
  
  ?> 

  <?php
  if($antbilder>0)
  {
     
  ?>
    
    <tr>
  <?php 
    $sql = "SELECT * FROM {$table6}";
    $rettmp2 = sqlsrv_query($conn,$sql); 
    $i=0;
    $bilde[ ]=array();
   while($row3 = sqlsrv_fetch_array($rettmp2)){
       
       $bilde1 = "data:image/jpg;base64,"; 
       $bilde2 = $row3['TransImage'];
       $bilde[$i] = $bilde1 . $bilde2; 
       $i=$i+1;
    ?>


  <?php } ?>
 

 <?php
   for($i=0;$i=$antbilder;$i++)
   { ?>

        <td valign="top" class="rapport_image1">
           <img src='<?php echo $bilde[$i] ?>' hight="190" align="left">
        </td>
   <?php  }?>


  <?php
    if($antbilder>0)
  {?></tr><?php }
}  ?> 
   
</table>    

</body>
</html>

Open in new window


So please explane what is so wrong with this code.

Thanks

I have many 1000 hours of coding, but not an expert
0
 
LVL 2

Author Closing Comment

by:team2005
ID: 39759620
thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question