PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: Retrieve images from mysql database using php

21 Mar 2015

Retrieve images from mysql database using php

                       Already you know  how to upload images to mysql database using php. Otherwise, visit upload images to mysql database using php.

Retrieve images from database:

                        Normally you can retrieve data from mysql table using php. Then you print the result. Similarly you retrieve image name from mysql database and print image name with located folder in '<img>' tag.

Consider following mysql table.


Retrieve images from mysql database

 The php script for retrieve images from mysql database is:


<?php
        mysql_connect('localhost','root','') or die(mysql_error());
        mysql_select_db('new')  or die(mysql_error());
        $q=mysql_query("select * from image where no=1 ")  or die(mysql_error());
        $res=mysql_fetch_array($q);
?>
    <img src="<?php echo 'http://localhost/upload/image/'.$res['img_name'];?>">

   
      where,
                 mysql_query() select the row which is no '1' from mysql database.
                 mysql_fetch_array() return the result an associated array. You want to know about  mysql fetch array().
                 In your img src print image name with location of image. Now you''ll get output like this:


phponwebsites

Related Post:

3 comments:

  1. At school I am using an older version of php but at home I am using php version 5.5.11 and mysql_fetch_array () is deprecated. Please can you tell me if there is an alternative function?

    ReplyDelete
  2. You can use mysql_fetch_assoc(), mysql_fetch_row() and mysql_fetch_object()

    ReplyDelete
  3. I used mysqli_fetch_array() and it worked, I converted all my mysql functions to mysqli! Thank you

    ReplyDelete

Note: only a member of this blog may post a comment.