0

i am trying to learn php & mysql. I have a table called image in the gallery database. I have stored 4 images in that table. I have this php script which should fetch images from the database. But on trying this on xampp server i am getting only one image from the database which is repeating 4 times. The code is :

<?php
session_START();
//$id = $_GET['id'];
$id = 1;
while ($id <= 4) {
    $link = mysql_connect("localhost", "root", "#")
    or die("Couldn't connect");
    mysql_select_db("gallery") or die("Couldn't connect");
    $sql = "SELECT img FROM image";
    $result = mysql_query("$sql");
    if ($result != 0) {
        $row = mysql_fetch_assoc($result);
        header("Content-type: image/jpeg");
        echo $row['img'];
        mysql_close($link);
    } else {
        echo("No data");
    }
    $id = $id + 1;
};
?>

I would be highly thankful to you if my problem gets resolved.

user3415653
  • 325
  • 3
  • 14
Harshit
  • 50
  • 7
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 24 '15 at 14:33

3 Answers3

0

You should really rewrite your loop to do only one query and then loop through the results, but to get it to work that way you need actually pass the id of the image you are trying to retrieve. Currently you are just grabbing the first image in the table, then running another query which grabs the first image, etc. etc.

Try this:

$sql = "SELECT img FROM image WHERE id = $id";

Hope that helps!

Chip Dean
  • 4,222
  • 3
  • 24
  • 36
  • Thank you Sir for looking into my concern. But, Sir on trying your suggestion also it is showing the same result i.e. only one image. It would be great to get more help from you. – Harshit Apr 24 '15 at 15:41
0

You can get all the images with below code.

<?php
session_START();
//$id = $_GET['id'];

    $link = mysql_connect("localhost", "root", "#") or die("Couldn't connect");
    mysql_select_db("gallery") or die("Couldn't connect");

    $sql = "SELECT img FROM image";
    $result = mysql_query("$sql");
    if (mysql_num_rows($result) > 0) {
        while($row = mysql_fetch_assoc($result)){
            header("Content-type: image/jpeg");
            echo $row['img'];
        }
        mysql_close($link);
    } else {
        echo("No data");
    }
?>

Regards,

Jayson
  • 1,105
  • 7
  • 25
  • Thank you Sir for loooking into my concern. But i am still having the same problem i.e. the code is still fetching single image from the database. What else should i try ? – Harshit Apr 24 '15 at 15:48
0

on Database first of all I would suggest that you do not store images in database but store the file name or file link.

on Folder function Thumbs() {

 $thumbs_path_from_root = 'images/galleryName/';
 foreach(glob($thumbs_path_from_root.'*.jpg') as $image){
    $id = explode('.', basename($image));
    echo "<div class='ecard_holder'><img src='".$image."' class='ecard_thumb' id='".$id[0]."' /></div>";
 }
}

this will search a folder you have specified and list all the images in a page you have used this function on

put this function "Thumbs()" in your html and style it well

Daniel PurPur
  • 519
  • 3
  • 13