Solved

Display pictures from database

Posted on 2013-12-18
7
182 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 108

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now