1

I have created a database using phpmyadmim and to access it, I made a name.php file.

Accessing the registration number, name was easy, but the image retrieval is not so. I stored the image via phpmyadmin using LongBlob as the type..but I'm not able to display it..

how to retrieve the image?

Please if someone can help,it would be high appreciated.

Thanks, LJ

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>PHYTOCHEMICAL DB</title>
        <style type="text/css">
            body {
                font-family: Georgia, "Times New Roman",
                    Times, serif;
                color: purple;
                background-color: #EEEEEE}
            </style>
        </head>
        <body>
            <table width="800" border="" align="center">
                <tr>
                 <td colspan="2" style="background-color:#FFA500;height:30px;width:700px">
                    <div> <img src="leaves.jpg" height="300" width="900"/> </div> <br/>
                </td>
            </tr>
            <tr>
                <td style="background-color:#FFD700;width:250px;">
                    <b>Menu</b><br>
                    <i> <a href="home.php">Home</a><br>
                        <i> <a href="did.php">Search by Database ID (DID)</a> <br>
                            <i><a href="mw.php">Search by Molecular weight<br>
                                    </td>

                              <td style="background color:#EEEEEE;height:500px;width:800px;">
                                        <form action="" method="POST">
                                            <p> Enter the name </p>
                                            <input type="text" name="pname">  
                                            <input type="submit" value="submit" name="submit1">  
                                            <br> 
                                        </form> 
                                        <?php
                                        $mysqli = new mysqli("localhost", "root", "osddosdd", "phychem");
                                        if (mysqli_connect_errno()) {
                                            printf("Connect failed:%s\n", mysqli_connect_error());
                                            exit();
                                        }
                                        if (isset($_POST['submit1'])) {
                                            $pname = $_POST['pname'];
                                            $query = ("select * from pchem where name LIKE '$pname'");
                                            $result = $mysqli->query($query);
                                            echo 'The retrieved query is:';
                                            echo "<table border='1'>
        <tr> 
            <th>DID</th>
            <th>Name</th> 
            <th>Molecular weight</th>
        </tr>";
                                            while ($row = $result->fetch_row()) {
                                                echo '<tr>';
                                                printf("<th>%d</th> <th> %s </th> <th> %2f </th>", $row[0], $row[1], $row[2]);
                                                echo '</tr>';
                                                echo '</table>';
                                            }
                                        }
                                        $mysqli->close();
                                        ?>
</td>
                                    </table>
                                    </body>
                                    </html>
  • 5
    Never ever store image inside database, store the name and path of image in database and store the image in filesystem. – Yogesh Suthar Oct 08 '13 at 04:59
  • wat u hv tried so for? why u need to store image into DB? u can store it in directory too..and just save image path into DB –  Oct 08 '13 at 04:59
  • As above, not good to store, but if you MUST: -> http://stackoverflow.com/questions/8289451/php-image-retrieval-from-mysql-blob-directly-into-img-tag – francisco.preller Oct 08 '13 at 04:59
  • 1
    @YogeshSuthar "Never" is pretty strong. There are a couple edge cases where DB storage makes sense. (This probably isn't one of them... just saying that it's possible where storing the assets in a DB is logical.) – Brad Oct 08 '13 at 05:00
  • possible duplicate of: http://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag –  Oct 08 '13 at 05:01
  • It's a bit difficult to help you when you don't even mention what API you're using to access your database. PDO? MySQLi? `mysql_*`? – Brad Oct 08 '13 at 05:01
  • @ Brad im using Mysqli. – learntocode Oct 08 '13 at 05:07

2 Answers2

0

don't store the images directly into the database as BLOBs. Just store them as files and only store the file names in the database.

Alok Nath
  • 141
  • 1
  • 6
  • while storing the image into mysql via phpmyadmin, it asks to choose the file,,Is this the way to give path of the image?? and after giving this path, what code is to added in the .php page?? – learntocode Oct 08 '13 at 11:32
-1

I have googled and found some links that would solve you problem. Refer these links :

http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html

http://www.coderslexicon.com/inserting-images-into-mysql-and-retrieving-them-using-php/

http://pujanpiya.com.np/?q=node/25

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69