Jump to content

Recommended Posts

i have worked my way through storing images in directory and storing the location in mySQL db.Now i was trying to make a Image gallery for just that particular user who had uploaded those images. i am having problem with that.

<?php
session_start();
$username = $_SESSION['UserName'] ;
echo "Username : ".$username."<br/><br/>";
$user=$username;//storing session in another variable
include('dbconnect.php');//Conncetion to Database

$qry = "SELECT * FROM UserImage WHERE UserName = '$user' ";
$result = mysql_query($qry,$connection);
$result2 = mysql_num_rows($result);
if($result2 == 0)
	{die('Username does not exist.');}
else{

	[b]/////I need help here.how do i make all those images display here[/b]


}

	}
?>

Link to comment
https://forums.phpfreaks.com/topic/180545-displaying-pictures/
Share on other sites

I'll start from here

$result = mysql_query($qry,$connection);

 

This is your data.. you may not be able to echo $result with it without and error, but it is your data.  It's more commonly referred to as a resource.  As in MySQL resource.

 

 

You see to have this part down... pretty straight forward

$result2 = mysql_num_rows($result);
   if($result2 == 0) {
       die('Username does not exist.');
  }else{
         /////Display the data in an image gallery.
  }

 

The displaying your data part consists of taking the RESOURCE and putting it into something that you CAN echo.  Like an Array... of Strings (text,letters,numbers)

 

Thankfully PHP has just the function to do that which is called mysql_fetch_array.  All you have to feed it is a mysql resource an it spits out an array.

mysql_fetch_array($result);

But we still can't echo that because... well, what would we echo... unless we just prepended it

echo mysql_fetch_array($result);

 

It needs to be stored to a variable.  Why not call it $row, since that is what is going to be in this variable.. the top row's data.  Until you call mysql_fetch_array again, then it will call the second row.  and so on and so on...

$result = mysql_query($qry,$connection);
$row = mysql_fetch_array($result);
echo $row['UserName']; // Prints the first user
$row = mysql_fetch_array($result);
echo $row['UserName']; // Prints the second user

 

That kind of coding could get to be quite the eyesore afterwhile though and is really illogical.. considering there's loops to take advantage of.

 

$result = mysql_query($qry,$connection);
while($row = mysql_fetch_array($result)) {
    echo $row['UserName'];
}

 

That would do the exact same thing.  Isn't that neater? Everything beyond that is pretty much basic HTML and CSS.

Link to comment
https://forums.phpfreaks.com/topic/180545-displaying-pictures/#findComment-952517
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.