954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

displaying images

I have created a image upload for the site i am working on i now want the uer to be able to click on a preview button and this takes them to a separate page where they can see the images that have been uploaded.

for this to happen would i have to send a query to the database to retrieve the information or can i simply display them using the variable name which was set for the images.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

Hi Kevin,
the basic question: do you store the images in a database or as files in a file system.

Also, what should I imagine when you say "the variable name which was set for the images"?

petr.pavel
Junior Poster
117 posts since Mar 2008
Reputation Points: 30
Solved Threads: 16
 

so you want to make a thumbnail? also you can make a field called "id" and match the id with the image location. and then select like that.

kishou
Junior Poster in Training
52 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 

best way to store an image is to upload it. Store its directory as a varchar in a database then simply echo it back when you need it :D
there are better and more complicated but this is by far the easiest method
:D

hope this helps

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

i think what you need is this:

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];

all this does is make a variable that holds the directory (ex: "uploads/filename.extension" )
and that you can store in the database as a varchar :D

if you want me to make you a script just ask, np lol im bored.

hope this helps

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

i have now tried to connect to my database which is set up on the server and i keep getting could not connect. i have db.php page which sets up the database and recall this by using include db.php

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

Hi Kevin,
there's no way we could check the problem remotely.
Either you have the login data wrong (server name, username, password, db name) or the script doesn't connect or I don't know. Can you attach both db.php and the main script?

petr.pavel
Junior Poster
117 posts since Mar 2008
Reputation Points: 30
Solved Threads: 16
 

here is the db.php code
<?php

$dbhost = 'www.acmeart.co.uk';
$dbusername = 'acmeart';
$dbpasswd = 'all0wn01';
$database_name = 'acmeartdb';
$sitepath = "http://www.acmeart.co.uk/mercury/";
$sitename = "Mercury Email Distribution System";
$adminemail = "mercury@acmeart.co.uk";

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");
?>

here is the code i am using to connect to the db.

<?php

include "db.php";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die line 168
("QUERY ERROR: ".mysql_error());

$db = mysql_select_db("$database_name", $connection)
or die("QUERY ERROR: ".mysql_error());

// Create table in my_db database
mysql_select_db("$database_name", $connection);
$sql = "CREATE TABLE images
(
imageID int NOT NULL AUTO_INCREMENT,
Broad1 varchar(50),
Broad2 varchar(50),
Broad3 varhar(50)
)";


if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("$database_name", $con);

mysql_query("INSERT INTO image (Broad1)
VALUES ('$thumb_name')");

mysql_query($sql,$con);

mysql_close($con);

?>

i keep getting the error

(Using password: NO) in /home/acmeart/public_html/mercury/upload.php on line 168 QUERY ERROR: i have labelled line 168 for you

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

i have just spotted a typo and i am just getting could not connect now

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

i have changed 2 typo's in the code in the $conn = mysql_connect($dbhost, $dbuser, $dbpass)

$dbuser= $dbusername

$dbpass= $dbpasswd

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

Hi Kevin,
you will have to change your db login data because now that you posted them here anyone can connect to your database and do whatever he wants.

I ran db.php and I got a valid connection.
To verify it I put this to see what was the mysql_pconnect() result.

var_dump($connection);

Because db.php already connects, you don't have to connect again in your main script. $connection will be available in the main script.
Frankly, you can just not use $connection at all - if you don't need more than one connection in your script.

You don't have to run mysql_select_db() each time before you use a database. It's enough to run it just once in db.php and it will stay selected. You only need to run mysql_select_db() again if you want to use a different database.

The error that you were looking for was $dbusername versus $dbuser - you define and use different variables. The proper solution is to remove the second connect attempt.

Also, you don't need to run mysql_close() at the script's end because you're using permanent connections mysql_pconnect() and they are not affected by mysql_close().

petr.pavel
Junior Poster
117 posts since Mar 2008
Reputation Points: 30
Solved Threads: 16
 

all the passwords and connection details will be changed this is only for testing purposes. thank you for the reply is was helpful.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You