my image database:
image_id
user_id
image(long blob-store images)
image_name...etc

on gallery.php i just want to print all the image of who ever is loged in. here is what i did:
first i got username and id of whoever is loged in.

$user = $_SESSION['username'];
$id = $_SESSION['user_id']; 

than i select from image(database) where user_id matchs id of who is loged in

$sql = "SELECT * FROM image WHERE user_id = $id ORDER BY image_id DESC";

than i used msql_query store in result

$result = mysql_query($sql);

now to print all the images: (need help with this)

while($row = mysql_fetch_assoc($result))
    {
            $image = $sql['image'];
        base64_decode($image);
        echo "$row[image]";       //here it print all of wired symbols. for ex a;kfljoewoi234jk
            $count++;
            if($count == 5)       //6 rows
            {
                echo "</tr><tr>"; // Start a new row at 6 cells
                $count = 0;
            }
    }

when i run this it only prints broken images. i think the problem is here $row[image]

<img src='$row[image]' width='200' height='200'></img> 

here is my full code
gallary.php

<?php
session_start();
include("connect.php");

$user = $_SESSION['username'];
$id = $_SESSION['user_id']; //get user_id who ever is loged in
if($user)  //is user is loged in 
{               
    //select images from database
    $sql = "SELECT * FROM image WHERE user_id = $id ORDER BY image_id DESC";

    /*** table which holds images ***/
    echo "your gallery<br/><br/><br/>";
    echo "<a href='upload.php'>upload image</a>";


    echo "<table>"; // Your table
    echo "<tr>"; // first table row
    $count = 0;
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result))
    {
            //header("Content-type: image/jpg");
            echo"
                $image = $sql['image'];
                base64_decode($image);
                echo "$row[image]";
                    ";
            $count++;
            if($count == 5)       //6 rows
            {
                echo "</tr><tr>"; // Start a new row at 6 cells
                $count = 0;
            }
    }
    echo "</tr>";
    echo "</table>";
    echo "<a href='index.php'>home</a><br/><br/><br/>";
}
?>

Recommended Answers

All 20 Replies

I think this section is wrong. (lines 24-27)

echo"
$image = $sql['image'];
base64_decode($image);
echo "$row[image]";
";

Try something like the following in its place.

echo '<td><img src="image.php?id='.$row['image_id'].'" /></td>';

then create a new file called image.php with the following content.

<?php
    session_start();
    include("connect.php");
    $sql = "SELECT image FROM image WHERE image_id = ".$_GET['id'];
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        echo base64_decode($row[image]);
    }
?>
commented: ty +0

i got broken links but no errors

i should del $sql from gallery side right? and while loop

image.php
<?php
    session_start();
    include("connect.php");

    $sql = "SELECT image FROM image WHERE userid = ".$_GET['id'];
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        echo base64_decode($row[image]);
    }
?>





on gallery.php
$sql = "SELECT * FROM image WHERE user_id = $id ORDER BY image_id DESC";
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result))
    {
    <img src="image.php?id='.$row['image_id'].'" />
    $count++;
            if($count == 5)       //6 rows
            {
                echo "</tr><tr>"; // Start a new row at 6 cells
                $count = 0;
            }
    }

try changing line 8 of image.php

$imgdata = base64_decode($row[image]);
header("Content-Type: image/jpeg");
header("Content-Length: ".strlen($imgdata));
echo $imgdata;

note: the actual content-type used must depend on the type of image stored. ie: jpg gif png etc.
See this list for more details.

commented: ty +2

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\login_test\image.php on line 13

header("Content-Type: image/jpeg");

does connect.php produce any output??

there can't be any output at all before setting the Header
Also remove that blank line (line 5 above) from image.php, as that may be generating empty output.

commented: ty +0

connect.php only outputs die(); function so if it fails it prints message but if it works find it doesnt out any thing.

also i want getting a error on 'id'. it said undefined variable id on image.php so i change to this

<?php
    session_start();
    include("connect.php");
    $id = $_SESSION['user_id']; 
    $sql = "SELECT image FROM image WHERE user_id = $id;
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = base64_decode($row[image]);
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;

    }
?>

and now only error is the T_STRING one

i should del $sql from gallery side right? and while loop

Sorry, forgot to respond to this part.

No, you still want the loop in your gallery page, as this is selecting all images for the logged in user, and formatting the HTML page output.

The image.php page is only returning the contents of a single image file for display.

Both parts must work together for the user to see all images they have uploaded. Does this make sense the way I have explained it?

now only error is the T_STRING

in your page above, line 5 does not have a closing quote " which may be causing the issue.

please also make sure there are no blank lines in connect.php

commented: t +0

echo $imgdata just printns broken links. same for gallery.php

do you think i might be bc some where iam using wrong variable?

you are missing the ' " ' double quoute on line 5.
Secondly I don't think base64_decode is necessary. Just check it out. I've worked with the same system before. And i directly echoed the data rather than a base64_decode.

Secondly I don't think base64_decode is necessary. Just check it out. I've worked with the same system before. And i directly echoed the data rather than a base64_decode

I believe you are correct, but you have to then specify the encoding type Header to be base64, if I'm not mistaken...?

@hwoarang

Post your whole code for both pages again please, I'll set it up localy and see if I can make it work.

commented: tyy +0
gallery.php
<?php
session_start();
include("connect.php");

$user = $_SESSION['username'];
$id = $_SESSION['user_id']; //get user_id who ever is loged in
if($user)  //is user is loged in 
{               
    //select images from database
    $sql = "SELECT * FROM image WHERE user_id = $id ORDER BY image_id DESC";
    $result = mysql_query($sql);

    /*** table which holds images ***/
    echo "your gallery<br/><br/><br/>";
    echo "<a href='upload.php'>upload image</a>";


    echo "<table>"; // Your table
    echo "<tr>"; // first table row
    $count = 0;
    while($row = mysql_fetch_assoc($result))
    {
            echo '
                <td>

                    <img src="image.php?id='.$row['image_id'].'" />
                </td>
                ';

            $count++;
            if($count == 5)       //6 rows
            {
                echo "</tr><tr>"; // Start a new row at 6 cells
                $count = 0;
            }
    }
    echo "</tr>";
    echo "</table>";
    echo "<a href='index.php'>home</a><br/><br/><br/>";
}
?>

image.php

<?php
    session_start();
    include("connect.php");
    $id = $_SESSION['user_id']; 
    $sql = "SELECT image FROM image WHERE user_id = $id";
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = base64_decode($row[image]);
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;

    }
?>

I believe that in image.php, your sql query is mistaken.
Each user would be having multiple images. thereby the if statement on line 7 would imply false There by printing no data.
Your aim should be to look for each image at a time. I have modified the code a bit to meet in accordance to your requirements. Let us know how it works out.

<?php
    session_start();
    include("connect.php");
    $id = $_GET['id']; 
    $sql = "SELECT image FROM image WHERE image_id = $id";
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = $row[image];
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;

    }
?>

Notice: Undefined index: image_id in C:\xampp\htdocs\login_test\image.php on line 4

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login_test\image.php on line 7

for error one i also tried user_id, image_id and id but it didnt worked.

<?php
    session_start();
    include("connect.php");
    $id = $_GET['id']; 
    $sql = "SELECT image FROM image WHERE image_id = $id";
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = base64_decode($row[image]);
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;

    }
?>

Umm, Seems like there's an error with our sql_query.
Please try this.

<?php
    session_start();
    include("connect.php");
    $id = $_GET['id']; 
    $sql = "SELECT image FROM image WHERE image_id = $id";
    $result = mysql_query($sql);
    if ($result==false)
    {
    echo mysql_error();
    }else if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = base64_decode($row[image]);
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;

    }
?>

Secondly, does gallery.php still have broken links?

Notice: Undefined index: id in C:\xampp\htdocs\login_test\image.php on line 5

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login_test\image.php on line 8
is $_GET['id'] //is this id a user_id or image_id

<?php
    session_start();
    include("connect.php");
    $id = $_GET['id']; 
    $sql = "SELECT image FROM image WHERE user_id = $id";   //also tried image_id didnt worked
    $result = mysql_query($sql);
      if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = base64_decode($row[image]);
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;
    }
?>

$_GET['id'] refers to the image id.

when you try to execute this , do it with something like this "http://localhost/image.php?id=1".
Secondly,
Please answer the following:
1) Are you sure you need to base64_decode your image data?
2) Am I being too confusing?

1) iam not sure if i need. some one had one online so i puted there.
2) nope, you both are really helpful.

also my image database has
image_id
user_id
image(long blob-store images)
image_name...etc

Oh alrighty :).
Okay, Please remove the base64_decode :).
Next try image.php?id=1
Then let me know If we have any error within this.

<?php
    session_start();
    include("connect.php");
    $id = $_GET['id']; 
    $sql = "SELECT image FROM image WHERE image_id = $id";
    $result = mysql_query($sql);
    if ($row = mysql_fetch_assoc($result)) //should only be one record
    {
        $imgdata = $row[image];
        header("Content-Type: image/jpeg");
        header("Content-Length: ".strlen($imgdata));
        echo $imgdata;

    }
?>

still same errors.
on gallery.php broken links
on image.php
Notice: Undefined index: id in C:\xampp\htdocs\login_test\image.php on line 4

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login_test\image.php on line 7

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

This warning indicates that your query has failed. Replace line 6 with:

$result = mysql_query($sql) or die(mysql_error());

Now you should see why it fails. You can post the error back here. As for the undefined index notice, you can solve that by changing line 4 to:

$id = isset($_GET['id']) ? $_GET['id'] : -1;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.