how to display data an image using php from a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rinsa
    New Member
    • Apr 2010
    • 4

    how to display data an image using php from a database

    hi..i have a database named abc and a table named xyz.....in the table i have fields such as id no:,name,addres s,profession... i need one more field that displays the image of the person along with the information...

    it should display in a table format or any other good format in a page so that the image and the person's information is well displayed...

    how can i do this in php...i am using php and mysql...please help asap...
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    from php connect to mysql and select the database
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    select the data from database
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    display the data in a format you like
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    for more details
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Regrds

    Comment

    • rinsa
      New Member
      • Apr 2010
      • 4

      #3
      hi below is the code that displays my data from the database...is there a code that i can add to this which will help me display the image of the members along with their information..

      Code:
      <?php
      
        $con = mysql_connect("localhost","root","");
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("speka", $con);
        
        $result = mysql_query("SELECT * FROM members");
        
        echo "<table border='1'>
      <tr>
      <th>IDNo:</th>
      <th>Name</th>
      <th>Course Studied </th>
      <th>Academic Year</th>
      <th>Address</th>
      <th>Home Address</th>
      <th>Employment/Profession</th>
      </tr>";
      
      while($row = mysql_fetch_array($result))
        {
        echo "<tr>";
        echo "<td>" . $row['ID No:'] . "</td>";
        echo "<td>" . $row['Name'] . "</td>";
        echo "<td>" . $row['Course Studied'] . "</td>";
        echo "<td>" . $row['Academic Year'] . "</td>";
        echo "<td>" . $row['Address'] . "</td>";
        echo "<td>" . $row['Home Address'] . "</td>";
        echo "<td>" . $row['Employment/Proffession'] . "</td>";
        echo "</tr>";
        }
      echo "</table>";
      
      mysql_close($con);
      ?>

      Comment

      • chathura86
        New Member
        • May 2007
        • 227

        #4
        i guess you store the image in the database

        Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology


        check the "Delivering an Image From a Database" section

        Regards

        Comment

        • rinsa
          New Member
          • Apr 2010
          • 4

          #5
          hi..thanks chathura for the reply... i tried adding the image url...but still its not displaying the image... :(:(:( ...its showing that image icon with a cut in between for all the members....
          please some1 help....

          Code:
          <?php
          
            $con = mysql_connect("localhost","root","");
          if (!$con)
            {
            die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("speka", $con);
            
            $result = mysql_query("SELECT * FROM members");
            
            echo "<table border='1'>
          <tr>
          <th>IDNo:</th>
          <th>Image</th>
          <th>Name</th>
          <th>Course Studied </th>
          <th>Academic Year</th>
          <th>Address</th>
          <th>Home Address</th>
          <th>Employment/Profession</th>
          </tr>";
          
          while($row = mysql_fetch_array($result))
            {
            echo "<tr>";
            echo "<td>" . $row['ID No:'] . "</td>";
            echo "<td>" .'<img src ="'.$imgurl.'">'; "</td>";
            echo "<td>" . $row['Name'] . "</td>";
            echo "<td>" . $row['Course Studied'] . "</td>";
            echo "<td>" . $row['Academic Year'] . "</td>";
            echo "<td>" . $row['Address'] . "</td>";
            echo "<td>" . $row['Home Address'] . "</td>";
            echo "<td>" . $row['Employment/Proffession'] . "</td>";
            echo "</tr>";
            }
          echo "</table>";
          
          mysql_close($con);
          ?>
          Last edited by Atli; Apr 10 '10, 11:14 AM. Reason: Added [code] tags.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Hey.

            rinsa, where exactly is the image stored? Is the image inside the database, or does the database contain a location on the file-system where the image is stored?

            If it is inside the database, you need to create a separate PHP file to fetch it. I exapain that process in this article. (See phase #4, specifically.)

            If it is on the file-system, you need only fetch the location from the database and print a <img>, like you try to do on line #28 of your code. However, it is not enough to just print a $imgurl variable. You need to actually define the variable before using it, or it will be empty and no image will be found.

            Comment

            • rinsa
              New Member
              • Apr 2010
              • 4

              #7
              hi atli...i have created a column in the table which has the name image and the datatype varchar225...i have given the location of the file-system where the image is stored in the image column....eg: C:\xampp\htdocs \HTML\images\im age1.jpg....and now in the coding how should i declare the imgurl variable?

              is it like $imgurl=C:\xamp p\htdocs\HTML\i mages;

              sorry i am new to php and mysql...thankyo u for helping me out...

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Ok, so you have an absolute path to the image. That's not ideal, but we can work around that. The problem is that when you put a <img> tag in your HTML, the src attribute has to point to a URL location where the image is found. An absolute, physical location doesn't work.

                When the browser gets a <img> tag, it reads the src attribute and sends another request to the server, requesting this image. Therefore, the src attribute has to point to a place where the image can be read via the HTTP server.

                If you use this tag in a page at http://localhost/index.php:
                Code:
                <img src="C:\xampp\htdocs\HTML\images\image1.jpg">
                The browser will try to fetch an image at:
                - http://localhost/C:\xampp\htdocs\HTML\images\image1.jpg
                Which will obviously not work.

                What you need to do is remove the C:\xampp\htdocs part of the page create a tag that reads:
                Code:
                <img src="HTML/images/image1.jpg">
                This will make the browser request:
                - http://localhost/HTML/images/image1.jpg
                Which, if I am understanding you setup correctly, will return the image.


                To make this happen in your code, you need to do the following:
                1. Inside the while loop on line #24, where you echo the row, you need to fetch the image location from the $row into a variable.
                2. Use the str_replace function to remove the C:\xampp\htdocs part of the location.
                3. Echo the altered image location into the <img> tag on line #28.


                To help you along, here is an example of how this can be done. You will need to adapt this to your own code.
                [code=php]<?php
                // Connect to a MySQL database.
                $dbLink = mysql_connect( 'host', 'user', 'pwd' );
                mysql_select_db ( 'myDb', $dbLink );

                // Fetch images from the database.
                $sql = "SELECT name, imagelocation FROM myImages";
                $result = mysql_query( $sql, $dbLink ) or die(mysql_error ());

                while($row = mysql_fetch_ass oc( $result )) {
                // Fetch the data for the current image.
                $name = htmlentities( $row['name'] );
                $imageLocation = $row['imageLocation'];

                // Remove the junk from the image location
                $junk = 'C:\\xampp\\htd ocs';
                $imageLocation = str_replace( $junk, '', $imageLocation );

                // Replace \ slashes with / slashes.
                $imageLocation = str_replace('\\ ', '/', $imageLocation) ;

                // Print the <img> tag.
                echo "<img src=\"{$imageLo cation}\" title=\"{$name} \">";
                }
                ?>[/code]

                Comment

                Working...